devondahon's avatar

unique() not applied in migration

In my migration, I have this column:

$table->foreignId('child_id')->references('id')->on('myschema.users')->nullable()->unique()->constrained();

Why unique() is not applied? I don't see it in the list of constaints in PgAdmin for this column.

0 likes
4 replies
Snapey's avatar

Isn't it applied as a unique index?

s4muel's avatar

check under Constraints -> Unique: Unique constraints!

devondahon's avatar

@snapey @s4muel Thanks for your replies, I finally fixed it by setting this constraint separately:

$table->unique('child_id');
1 like
s4muel's avatar

@devondahon Oh, I see. The problem was probably due to the order of the methods because the references() (or on()) method returns a ForeignKeyDefinition. So it was probably “too late” to set nullable() or unique(). I haven’t tested it though. Give it a try.

Please or to participate in this conversation.