FIX: fix missing FK in contents->templates
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddFKToContenctAndTemplates extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$platform = Schema::getConnection()
|
||||
->getDoctrineSchemaManager()
|
||||
->getDatabasePlatform();
|
||||
$platform->registerDoctrineTypeMapping('enum', 'string');
|
||||
|
||||
Schema::table('contents', function (Blueprint $table) {
|
||||
$table->string('template', 128)
|
||||
->nullable(true)
|
||||
->comment('Template used for rendering this content page.')
|
||||
->change();
|
||||
});
|
||||
|
||||
DB::update('UPDATE contents SET template = NULL WHERE template NOT IN (SELECT DISTINCT template_name FROM templates)');
|
||||
|
||||
// add FK
|
||||
Schema::table('contents', function (Blueprint $table) {
|
||||
$table->foreign('template')
|
||||
->references('template_name')
|
||||
->on('templates')
|
||||
->onDelete('set null')
|
||||
->onUpdate('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$platform = Schema::getConnection()
|
||||
->getDoctrineSchemaManager()
|
||||
->getDatabasePlatform();
|
||||
$platform->registerDoctrineTypeMapping('enum', 'string');
|
||||
|
||||
Schema::table('contents', function (Blueprint $table) {
|
||||
$table->dropForeign('contents_template_foreign');
|
||||
});
|
||||
|
||||
Schema::table('contents', function (Blueprint $table) {
|
||||
$table->string('template', 128)
|
||||
->nullable(false)
|
||||
->change();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user