问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/51605107/how-can-i-add-db-level-unique-constraint-to-a-column-in-a-table-in-rails-using-t