Uniqueness in Migration

I am trying to find a way to achieve this, but I cannot find any attempts, even so I think that maybe my approach is completely wrong. However, what should I do in my migration if I want the combination of the two fields to be unique? Note that I do not want them to be indexes, just database fields.

For example, to go below, I can separately add unique: true to the fields, but combos?

 class CreateSomething < ActiveRecord::Migration def change create_table :something do |t| t.date :datestamp, :null => false t.integer :some_number, :null => false t.timestamps end end end 
+5
source share
1 answer

I'm not sure what you mean by

Note that I do not want them to be indexes, just database fields.

Indexes are additional pieces of information that the database stores column data. More importantly, the index is exactly what you need!

 class CreateSomething < ActiveRecord::Migration def change create_table :something do |t| t.date :datestamp, :null => false t.integer :some_number, :null => false t.timestamps end add_index :something, [:datestamp, :some_number], unique: true end end 
+9
source

Source: https://habr.com/ru/post/1204271/


All Articles