Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

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

0 likes
5 replies
martinbean's avatar

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.

1 like
vincent15000's avatar

@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.

Tray2's avatar

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

1 like
martinbean's avatar

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…

2 likes

Please or to participate in this conversation.