I am trying to create a pivot table, with foreign keys, this is the migration I have made in Laravel:
public function up()
{
Schema::create('player_position', function (Blueprint $table) {
$table->integer('player_id')->unsigned()->index();
$table->integer('position_id')->unsigned()->index();
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
$table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');
});
}
But, I get an error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table player_position add constraint
player_position_posi tion_id_foreign foreign key (position_id)
references positions (id) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
I have read that usually foreign key constraint errors are about not assigning unsigned to fields, or already having records in the DB, but my DB is empty, and I my fields have unsigned, so don't know what is the problem?
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
For a field to be defined as a foreign key, the referenced parent field must have an index defined on it. And the datatype of both the column and its size must be the same.
I think you are violating some of the above rule.
Remove ->index() method as it creates basic index while you want to add foreign key constraint which references primary key on another table.
$table->integer('player_id')->unsigned();
$table->integer('position_id')->unsigned();
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
$table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');
Related
I have two tables. The first one is a pivot table (with two composite keys). I want to add a foreign key in my second table and that foreign key is based on the pivot table. When I run the migration it causes an exception
SQLSTATE[HY000]: General error: 1822 Failed to add the foreign key constraint.
Missing index for constraint 'property_chqeque_first_rows_landlord_agreement_id_foreign'
in the referenced table 'landlord_agreements' (SQL: alter table`property_chqeque_first_rows`
add constraint
`property_chqeque_first_rows_landlord_agreement_id_foreign` foreign key
(`landlord_agreement_id`) references `landlord_agreements` (`agreements_id`))
Here is my two migration files
pivot table
Schema::create('landlord_agreements', function (Blueprint $table) {
$table->primary(['landlords_id', 'agreements_id']);
$table->unsignedBigInteger('landlords_id');
$table->unsignedBigInteger('agreements_id');
$table->string('created_by');
$table->string('updated_by')->nullable();
$table->timestamps();
$table->softDeletes();
});
The migration for adding the column and foreign key in second table
Schema::table('property_chqeque_first_rows', function (Blueprint $table) {
$table->unsignedBigInteger('landlord_agreement_id')->after('id')->nullable();
$table->foreign('landlord_agreement_id')->references('agreements_id')->on('landlord_agreements');
});
Can someone tell me where I did go wrong? I always use this method when I create a foreign key but this time it doesn't migrate. I was thinking to just add a column without a foreign key link.
Make agreements_id unique in the table landlord_agreements
or add index
$table->index(['agreements_id']);
I have a table named "countries" and another table named "country_continents" in my DB. I want to make my continent_id column in countries , a foreign key referencing the id of country_continents, but I am getting an error message. This is the SQL to create the foreign key and the error:
ALTER TABLE countries
ADD CONSTRAINT fk_continent_id
FOREIGN KEY (continent_id)
REFERENCES country_continents(id);
ERROR:
#1215 - cannot add foreign key constraint
At first, I was getting the :
"Error: relational features are disabled"
so I ran the command ALTER TABLE countries ENGINE=InnoDB; and ALTER TABLE country_continents ENGINE=InnoDB; but now I'm getting the #1215 error.
This is the Structure for "country_continents":
This is the Structure for "countries":
Any ideas on whats happening? Thanks in advance.
I think the error is due to:
in your countries table, continent_id is not unsigned.
Edit that and tell me if it worked
I am using MariaDB and the DB schema looks like this:
And I've created those tables:
However, I didn't defined the foreign key when I created the table. But I am trying to use alter manipulation command to make a reference between two tables like this:
ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES follow(uid);
ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES follow (following_uid);
ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES feed (uid);
The first one was successful. But the other ones, I get this error:
Can't create table `SimpleSNS`.`#sql-6b94_5f` (errno: 150 "Foreign key constraint is incorrectly formed")
I checked InnoDB and the columns data type and its prototypes such as default or not null, etc. However, It didn't work.
What's the problem of this???
Create an index on "follow (following_uid)" before executing -> ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES follow (following_uid);
I have created a new migration to add a column to an existing table and add a foreign key to an existing table.
This is the migration with the new column:
Schema::table( 'events', function ( Blueprint $table ) {
$table->integer( 'category_id' )->unsigned()->after( 'place_id' );
$table->foreign('category_id')->references('id')->on('categories');
} );
When I run the migrate command I get:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meetmount`.`#sql-3c8_424`, CONSTRAINT `events_catego
ry_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)) (SQL: alter table `events` add constraint events_category_id_foreign foreign key (`category_id`) r
eferences `categories` (`id`))
and
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meetmount`.`#sql-3c8_424`, CONSTRAINT `events_catego
ry_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`))
How can I solve?
The cause of the error could be the following:
The parent table categories and/or the child table events already have records in them. As you want to reference the parent table's particular column (id), MySQL will expect the child table to have a value that exists in the parent, hence the integrity constraint violation.
A possible solution (in case the parent is not empty) is to add a default value in the events table for the foreign key column:
$table->integer( 'category_id' )->unsigned()->after( 'place_id' );
$table->foreign('category_id')->references('id')->on('categories')->default(1);
Happened the same with me, but I resolved this adding a default value on creation of column:
$table->integer( 'category_id' )->unsigned()->after( 'place_id' )->default(1);
Make sure the table "categories" has a record with id = 1.
alter table xxx
add CONSTRAINT `FK_usagehistory_4` FOREIGN KEY (`AuthID`) REFERENCES `licenseattributes` (`AuthID`),
add CONSTRAINT `FK_usage_5` FOREIGN KEY (`SaaSClientIdentifierID`) REFERENCES `saasclientnodes` (`SaaSClientIdentifierID`),
add CONSTRAINT `FK_usage_6` FOREIGN KEY (`SaaSServerIdentifierID`) REFERENCES `saasservernodes` (`SaaSServerIdentifierID`),
add CONSTRAINT `FK_usage_9` FOREIGN KEY (`TenantID`) REFERENCES `tenant` (`TenantID`);
I am getting ERROR "1005 (HY000): Can't create table 'db.#sql-5471_137' (errno: 121)" while runng above command .
The error seems to be because you are creating a a foreign key constraint and you need to have a usable index in referencing table and in the referenced table to do so in MySQL. Probably the index on the referenced table would be missing which you have to create by yourself. The index on the referencing table will be created automatically, so no need to create that.
This problem had been solved. I had changed fk name as its was same as pk. That's why I was getting this error.