Laravel - removing foreign key and adding one migration
I'm trying to drop a foreign key and add a foreign key in one migration, but I can't get it to work: I created a table contents
with foreign key cms_id
:
public function up()
{
Schema::create('contents', function (Blueprint $table) {
$table->increments('id');
$table->integer('ct_id')->unsigned();
$table->foreign('ct_id')->references('id')->on('content_types');
$table->integer('cms_id')->unsigned();
$table->foreign('cms_id')->references('id')->on('inventories');
$table->string('title');
$table->string('slug');
$table->text('excerpt');
$table->mediumText('body');
$table->string('password');
$table->integer('parent_id');
$table->timestamps();
});
}
This is what the inventory table looks like:
Schema::create('inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('remote_id');
$table->integer('local_id')->unsigned();
$table->string('local_type');
$table->timestamps();
});
And after it was unable to delete it and create a new foreign key, I deleted all tables in the DB and tried to run all migrations again with the new one:
public function up()
{
Schema::table('inventories', function (Blueprint $table) {
$table->integer('remote_id')->unsigned()->change();
});
Schema::table('contents', function (Blueprint $table) {
$table->dropForeign('contents_cms_id_foreign');
$table->foreign('cms_id')->references('remote_id')->on('inventories');
});
Schema::table('files', function (Blueprint $table) {
$table->dropForeign('files_cms_id_foreign');
$table->foreign('cms_id')->references('remote_id')->on('inventories');
});
}
But I am getting:
[Light up \ Database \ QueryException]
SQLSTATE [HY000]: General Error: 1215 Unable to add foreign key constraint (SQL: modify tablecontents
addcontents_cms_id_foreign
foreign k ey (cms_id
) constraint referenceinventories
(remote_id
))[PDOException]
SQLSTATE [HY000]: General error: 1215 Unable to add foreign key constraint
If I first run all migrations where I create all tables and then after that I run a new migration where I change foreign keys, I get this error:
[Light up \ Database \ QueryException]
SQLSTATE [42000]: Syntax error or access violation: 1091 cannot DROP 'content s_cms_id_foreign'; check if column / key exists (SQL: alter tablecontents
delete foreign keycontents_cms_id_foreign
)[PDOException]
SQLSTATE [42000]: syntax error or access violation: 1091 cannot DROP 'content s_cms_id_foreign'; make sure the column / key exists
I even tried to just delete all tables in the DB and only edit the files that I already have, just changing without new migrations:
$table->integer('ct_id')->unsigned();
$table->foreign('ct_id')->references('remote_id')->on('content_types');
$table->integer('cms_id')->unsigned();
$table->foreign('cms_id')->references('remote_id')->on('inventories');
And also in the stock table:
$table->integer('remote_id')->unsigned();
But even then it doesn't work and I get:
[Light up \ Database \ QueryException]
SQLSTATE [HY000]: General Error: 1215 Unable to add foreign key constraint (SQL: modify tablecontents
addcontents_cms_id_foreign
foreign k ey (cms_id
) constraint referenceinventories
(remote_id
))[PDOException]
SQLSTATE [HY000]: General Error: 1215 Unable to add foreign key constraint
So, I checked all the usual suspects that I came across on the internet, I even added $table->engine = 'InnoDB'
; to the tables inventories
, contents
and files
, I first created tables
and then add foreign keys
and also checked if the columns have the same data type
, not sure what to do otherwise?
source to share
No one has answered this question yet
Check out similar questions: