IT

열 조합에 고유 제한 조건을 추가하기위한 마이그레이션

lottoking 2020. 7. 1. 07:41
반응형

열 조합에 고유 제한 조건을 추가하기위한 마이그레이션


필요한 것은 열 조합에 고유 제약 조건을 적용하는 마이그레이션입니다. A에 대한 즉 people테이블의 조합 first_name, last_Name그리고 Dob고유해야합니다.


add_index :people, [:firstname, :lastname, :dob], :unique => true


howmanyofme.com에 따르면 미국에만 "John Smith라는 46,427 명의 사람들이있다"고합니다. 약 127 년입니다. 이것이 인간의 평균 수명보다 훨씬 길기 때문에 이것은 DOB 충돌이 수학적으로 확실하다는 것을 의미합니다.

내가 말하고 싶은 것은 고유 필드의 특정 조합이 미래에 극단적 인 사용자 / 고객 불만을 야기 할 수 있다는 것입니다.

적절한 경우 국가 식별 번호와 같이 실제로 고유 한 것을 고려하십시오.

(나는이 파티에 매우 늦었다는 것을 알고 있지만 앞으로 독자에게 도움이 될 수 있습니다.)


인덱스없이 제약 조건을 추가 할 수 있습니다. 사용중인 데이터베이스에 따라 다릅니다. 다음은 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

추가 할 수있는 다른 구속 조건이 있습니다. 문서를 읽으십시오


안녕 당신은 예를 들어 열에 마이그레이션에서 고유 색인을 추가 할 수 있습니다

add_index(:accounts, [:branch_id, :party_id], :unique => true)

또는 각 열에 대해 별도의 고유 색인


사용자와 게시물 간 조인 테이블의 일반적인 예에서 :

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

두 개의 유사한 레코드를 만들려고하면 데이터베이스 오류가 발생합니다 (필자의 경우 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"

예를 들면 :

Ownership.create!(user_id: user_id, post_id: post_id)
Ownership.create!(user_id: user_id, post_id: post_id)

완전히 실행 가능한 예 : https://gist.github.com/Dorian/9d641ca78dad8eb64736173614d97ced

db/schema.rb생성 : https://gist.github.com/Dorian/a8449287fa62b88463f48da986c1744a

참고 URL : https://stackoverflow.com/questions/3370271/a-migration-to-add-unique-constraint-to-a-combination-of-columns

반응형