Ruby on rails 为什么复合主键不作为外键添加到psql(rails应用程序)中?

Ruby on rails 为什么复合主键不作为外键添加到psql(rails应用程序)中?,ruby-on-rails,database,foreign-keys,psql,composite-primary-key,Ruby On Rails,Database,Foreign Keys,Psql,Composite Primary Key,我正在使用activerecord多租户gem在rails项目中实现多租户 遵照 我有一个用户模型,考勤模型,属于用户,以及作为租户的公司 当我将用户的主键(id)更新为复合键(id,公司id)时,它正在更新 但是,当我试图更新考勤模型中的外键时,它只是试图将用户id作为fk并搜索其唯一约束 下面是我使用的命令- execute 'ALTER TABLE users DROP CONSTRAINT users_pkey;' execute 'ALTER TABLE users ADD CONST

我正在使用
activerecord多租户
gem在rails项目中实现多租户

遵照

我有一个
用户
模型,
考勤
模型,属于
用户
,以及作为租户的
公司

当我将用户的
主键(id)
更新为
复合键(id,公司id)
时,它正在更新

但是,当我试图更新
考勤
模型中的外键时,它只是试图将
用户id
作为fk并搜索其唯一约束

下面是我使用的命令-

execute 'ALTER TABLE users DROP CONSTRAINT users_pkey;'
execute 'ALTER TABLE users ADD CONSTRAINT users_pkey PRIMARY KEY(id, company_id);'

execute 'ALTER TABLE attendances DROP CONSTRAINT attendances_pkey;'
execute 'ALTER TABLE attendances ADD CONSTRAINT attendances_pkey PRIMARY KEY (id, company_id);'
execute 'ALTER TABLE attendances ADD FOREIGN KEY(user_id, company_id) REFERENCES users(id, company_id);'
schema:load时出现的错误

ActiveRecord::StatementInvalid: PG::InvalidForeignKey: ERROR:  there is no unique constraint matching given keys for referenced table "users"
: ALTER TABLE "attendances" ADD CONSTRAINT "attendances_user_id_company_id_fkey"
FOREIGN KEY ("user_id")
  REFERENCES "users" ("id")


解决方案是使用
structure.sql
文件。 在
schema.rb
文件中,它不能始终遵循sql命令并反映在数据库中。因此,由于模式无法正确加载sql命令,因此我收到了此错误
InvalidForeignKey

在移动到
structure.sql
之后,所有迁移都在文件中正确运行,并使用了精确的命令