ran*_*alo 132 database ruby-on-rails
我需要的是迁移以将唯一约束应用于列的组合.即对于一个people
表,一个组合first_name
,last_Name
并且Dob
应该是唯一的.
Rob*_*her 233
add_index :people, [:firstname, :lastname, :dob], :unique => true
A F*_*kly 23
根据howmanyofme.com,"仅在美国就有46,427人名叫约翰史密斯".那是大约127年的日子.由于这远远超过了人类的平均寿命,这意味着DOB冲突在数学上是确定的.
我只是说,独特领域的特殊组合可能会导致未来极端的用户/客户挫败感.
如果合适的话,考虑一些实际上独特的东西,如国家识别号码.
(我意识到我参加派对的时间已经很晚了,但它可以帮助未来的读者.)
Jos*_*osh 19
您可能希望添加没有索引的约束.这取决于您使用的数据库.以下是Postgres的示例迁移代码. (tracking_number, carrier)
是要用于约束的列的列表.
class AddUniqeConstraintToShipments < ActiveRecord::Migration
def up
execute <<-SQL
alter table shipments
add constraint shipment_tracking_number unique (tracking_number, carrier);
SQL
end
def down
execute <<-SQL
alter table shipments
drop constraint if exists shipment_tracking_number;
SQL
end
end
Run Code Online (Sandbox Code Playgroud)
您可以添加不同的约束. 阅读文档
Kha*_*oui 12
为了完整起见,并避免混淆,这里有 3 种方法可以做同样的事情:
在 Rails 5.2+ 中向列组合添加命名的唯一约束
假设我们有一个属于广告商的 Locations 表,并且有列 reference_code,并且您只需要每个广告商 1 个参考代码。所以您想向列组合添加唯一约束并为其命名。
做:
rails g migration AddUniquenessConstraintToLocations
并使您的迁移看起来像这样的一个班轮:
class AddUniquenessConstraintToLocations < ActiveRecord::Migration[5.2]
def change
add_index :locations, [:reference_code, :advertiser_id], unique: true, name: 'uniq_reference_code_per_advertiser'
end
end
Run Code Online (Sandbox Code Playgroud)
或者这个块版本。
class AddUniquenessConstraintToLocations < ActiveRecord::Migration[5.2]
def change
change_table :locations do |t|
t.index ['reference_code', 'advertiser_id'], name: 'uniq_reference_code_per_advertiser', unique: true
end
end
end
Run Code Online (Sandbox Code Playgroud)
或者这个原始 SQL 版本
class AddUniquenessConstraintToLocations < ActiveRecord::Migration[5.2]
def change
execute <<-SQL
ALTER TABLE locations
ADD CONSTRAINT uniq_reference_code_per_advertiser UNIQUE (reference_code, advertiser_id);
SQL
end
end
Run Code Online (Sandbox Code Playgroud)
其中任何一个都会有相同的结果,请检查您的 schema.rb
嗨您可以在迁移到列时添加唯一索引
add_index(:accounts, [:branch_id, :party_id], :unique => true)
Run Code Online (Sandbox Code Playgroud)
或为每列分隔唯一索引
在用户和帖子之间的连接表的典型示例中:
create_table :users
create_table :posts
create_table :ownerships do |t|
t.belongs_to :user, foreign_key: true, null: false
t.belongs_to :post, foreign_key: true, null: false
end
add_index :ownerships, [:user_id, :post_id], unique: true
Run Code Online (Sandbox Code Playgroud)
尝试创建两个相似的记录将引发数据库错误(在我的情况下为 Postgres):
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_ownerships_on_user_id_and_post_id"
DETAIL: Key (user_id, post_id)=(1, 1) already exists.
: INSERT INTO "ownerships" ("user_id", "post_id") VALUES ($1, $2) RETURNING "id"
Run Code Online (Sandbox Code Playgroud)
例如这样做:
Ownership.create!(user_id: user_id, post_id: post_id)
Ownership.create!(user_id: user_id, post_id: post_id)
Run Code Online (Sandbox Code Playgroud)
完全可运行的例子:https : //gist.github.com/Dorian/9d641ca78dad8eb64736173614d97ced
db/schema.rb
生成:https : //gist.github.com/Dorian/a8449287fa62b88463f48da986c1744a