3

Is it possible to create unique constraints on active record with value of field.

So this code will add a constraints of 2 columns:

add_index :my_table,[:field_1,:field_2],:unique => true

But I would like to have the same constrains where field_2 == true

For example something like this

add_index :my_table,[:field_1,:field_2 => true],:unique => true

Is it possible ? I saw that it is possible on postgres

1 Answer 1

3

For partial indexes you can use the where option:

add_index :my_table, [:field_1, :field_2], unique: true, where: 'field_2 = true'
# "index_my_tables_on_field_1_and_field_2" UNIQUE, btree (field_1, field_2) WHERE field_2 = true
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.