Ruby on rails 从rails中引用外键记录的表中删除主键
如果唯一标识符是Ruby on rails 从rails中引用外键记录的表中删除主键,ruby-on-rails,ruby,rails-migrations,Ruby On Rails,Ruby,Rails Migrations,如果唯一标识符是user\u id外键,是否需要我的id列?我想我永远不会按id查询user\u profile。原因是我的用户模型有一个:user\u profile,我的user\u profile属于:user 模型 class User < ApplicationRecord has_one :user_profile end class UserProfile < ApplicationRecord belongs_to :user end 您可以将表的任何
user\u id
外键,是否需要我的id
列?我想我永远不会按id查询user\u profile
。原因是我的用户模型有一个:user\u profile
,我的user\u profile属于:user
模型
class User < ApplicationRecord
has_one :user_profile
end
class UserProfile < ApplicationRecord
belongs_to :user
end
您可以将表的任何属性称为主键,如下所示:
create_table :user_profiles, :primary_key => :user_id do |t|
Table "public.user_profiles"
Column | Type | Collation | Nullable | Default
------------+--------------------------------+-----------+----------+-------------------------------------------
id | bigint | | not null | nextval('user_profiles_id_seq'::regclass)
first_name | character varying | | |
last_name | character varying | | |
created_at | timestamp(6) without time zone | | not null |
updated_at | timestamp(6) without time zone | | not null |
users_id | bigint | | |
Indexes:
"user_profiles_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk_rails_1e573f2ed5" FOREIGN KEY (users_id) REFERENCES users(id)
create_table :user_profiles, :primary_key => :user_id do |t|