Foreign ULID fails with constrained()
Hello,
I have this code.
$table->foreignUlid('type_id')->constrained();
SQLSTATE[HY000]: General error: 1005 Can't create table mpa.events (errno: 150 "Foreign key constraint is incorrectly formed") (Connection: mariadb, SQL: alter table events add constraint events_type_id_foreign foreign key (type_id) references types (id))
And it fails.;
It's with MariaDB.
Do you have any idea why it fails ?
Thanks for your help.
V
Do you have any idea why it fails ?
@vincent15000 Not without seeing the schemas of the two tables you’re trying to create a foreign key between.
@martinbean The schema was correct, but the model was declared as a Pivot instead of a Model.
Probably a typo problem when I created the model from the CLI.
You need to make sure the primary key and the foreign key are of the same data type.
For example you can't have one that is unsigned big integer and unsigned integer, they need to be exactly the same.
So my guess here is that you are using different data types on them.
And I would recommend using integers instead of ulid, it will be faster and easier to index
You need to make sure the primary key and the foreign key are of the same data type.
@Tray2 Indeed. Which is why I asked @vincent15000 to actually show the schemas of the two tables…
@Tray2 @martinbean That was already clear for me, the problem came from something else as mentioned in my previous comment.
Please or to participate in this conversation.