在 Rails 中给出复合主键 [英] Give composite primary key in Rails

查看:40
本文介绍了在 Rails 中给出复合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有任何 gem 的情况下在 Rails 中提供复合主键?

我在迁移文件中的第一个表:

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

在我的架构文件中:

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 个外键.所以我试过这样:

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

现在如何将 user_id 和 project_id 的组合设置为 user_has_projects 中的主键?

Now how can I set combination of user_id and project_id as a primary key in user_has_projects?

推荐答案

看起来您正在尝试指定 UsersProjects 之间的多对多关系,与关系本身的附加字段.

It looks like you're trying to specify a many-many relationship between Users and Projects, with an additional field on the relationship itself.

您目前的做法不是 Rails 的做事方式 - 特别是使用复合主键的概念.

The way you're currently doing isn't the Rails way of doing things - especially with the concept of a composite primary key.

Rails/ActiveRecord 进行这种关系建模的方法是使用第三个模型来描述 UserProject 之间的关系.作为示例,我将其称为 Assignment.您需要做的就是将您的 user_has_projects 表重命名为 assignments,如下所示:

The Rails/ActiveRecord way of doing this sort of relationship modelling is to have a third model that describes the relationship between User and Project. For the sake of example, I'm going to call it an Assignment. All you need to do is re-name your user_has_projects table to assignments like so:

class CreateAssignments < ActiveRecord::Migration
  def self.up
    create_table :assignments do |t|
      t.references :user
      t.references :project
      t.boolean :status
      t.timestamps
    end
  end

  def self.down
    drop_table :assignments
  end
end

然后,在您的模型文件中:

And then, in your model files:

# app/models/user.rb
class User < ActiveRecord::Base
  has_many :assignments
  has_many :projects, :through => :assignments
end

# app/models/assignment.rb
class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :project
end

# app/models/project.rb
class Project < ActiveRecord::Base
  has_many :assignments
  has_many :users, :through => :assignments
end

您可以在此处阅读更多相关信息:http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

You can read more about this here: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

这篇关于在 Rails 中给出复合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆