Henry Yang
Henry Yang

Reputation: 2583

How can I add DB level Unique constraint to a column in a table in Rails using the migration file?

How can I add DB level unique constraint to a column in a table in Rails using its migration? I googled but those answer seems to involve index, and I don't want to touch index because I'm not sure if it will have bad side effect (because stackoverflow.com/a/3370333/6359753 has a comment saying it will have storage influence). Do I must have it and will it have bad side effect?

This is why even though I have read A migration to add unique constraint to a combination of columns but still asking this question.

Upvotes: 0

Views: 438

Answers (1)

matthewd
matthewd

Reputation: 4420

Yes, the unique index is a necessary part of a unique constraint: the index is how the constraint is enforced [efficiently].

Creating a unique index on its own doesn't always technically create a constraint, but that is almost always an irrelevant distinction.

Upvotes: 1

Related Questions