在 Rails 中给出复合主键
如何在不使用任何 gem 的情况下在 Rails 中提供复合主键?
迁移文件中的第一个表:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :userid
t.string :name
t.string :address
t.timestamps
end
end
def self.down
drop_table :users
end
end
迁移文件中的第二个表:
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.string :title
t.string :description
t.timestamps
end
end
def self.down
drop_table :projects
end
end
在我的架构文件中:
ActiveRecord::Schema.define(:version => 20110222044146) do
create_table "projects", :force => true do |t|
t.string "title"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "userid"
t.string "name"
t.string "address"
t.datetime "created_at"
t.datetime "updated_at"
end
end
现在我想要创建一个名为 User_has_project
的表,在其中我将引用 User 和 Project,这意味着将有 2 个外键。 所以我尝试这样:
class CreateUser_has_projects < ActiveRecord::Migration
def self.up
create_table :user_has_projects do |t|
t.references :User
t.references :Project
t.boolean :status
t.timestamps
end
end
def self.down
drop_table :users
end
end
现在如何将 user_id 和 project_id 的组合设置为 user_has_projects 中的主键?
How can i give composite primary key in Rails without any gem?
My first table in migration file:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :userid
t.string :name
t.string :address
t.timestamps
end
end
def self.down
drop_table :users
end
end
My second table in migration file:
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.string :title
t.string :description
t.timestamps
end
end
def self.down
drop_table :projects
end
end
In my schema file:
ActiveRecord::Schema.define(:version => 20110222044146) do
create_table "projects", :force => true do |t|
t.string "title"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "userid"
t.string "name"
t.string "address"
t.datetime "created_at"
t.datetime "updated_at"
end
end
Now I want to create a table called User_has_project
in which I will refer to User and Project that means will have 2 foreign keys.
So I tried like this:
class CreateUser_has_projects < ActiveRecord::Migration
def self.up
create_table :user_has_projects do |t|
t.references :User
t.references :Project
t.boolean :status
t.timestamps
end
end
def self.down
drop_table :users
end
end
Now how can I set combination of user_id and project_id as a primary key in user_has_projects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在尝试指定
Users
和Projects
之间的多对多关系,并在关系本身上添加一个附加字段。您当前所做的方式不是 Rails 的做事方式 - 尤其是复合主键的概念。
Rails/ActiveRecord 进行此类关系建模的方法是使用第三个模型来描述
User
和Project
之间的关系。为了举例,我将其称为作业
。您需要做的就是将您的user_has_projects
表重命名为assignments
,如下所示:然后,在您的模型文件中:
您可以在此处阅读有关此内容的更多信息:http://guides.rubyonrails.org/association_basics.html#the-has_many-through-协会
It looks like you're trying to specify a many-many relationship between
Users
andProjects
, with an additional field on the relationship itself.The way you're currently doing isn't the Rails way of doing things - especially with the concept of a composite primary key.
The Rails/ActiveRecord way of doing this sort of relationship modelling is to have a third model that describes the relationship between
User
andProject
. For the sake of example, I'm going to call it anAssignment
. All you need to do is re-name youruser_has_projects
table toassignments
like so:And then, in your model files:
You can read more about this here: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association