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

我与影子孤独终老i 提交于 2019-12-11 17:41:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!