I wanted like to add unique constraint to column name
in existing table psql
created table (PostgreSQL) :
class CreateRequests < ActiveRecord::Migration
def change
create_table :requests do |t|
t.integer :user_id
t.string :name
t.string :address
t.timestamps null: true
end
add_index :requests, :user_id
end
end
So I added validation uniqueness in model
class Request < ModelBase
belongs_to :user
validates :user, presence: true
validates :name, uniqueness: true, allow_blank: true
...
and migration like this:
def change
add_index :user_requests, :name, unique: true
end
but I noticed that in some cases name can be empty can I add_index with condition were :name
is null / not null?
Edit: yes, I want those empty values to stay in the database (I don't need to modify past request). I think that I need to edit migration to be more accurate to the actual state of db.
add_index :user_requests, :name, unique: true
I'm thinking about sth like if name is added should be unique (but can be empty) – eudaimonia