在 Rails 中给出复合主键

发布于 2024-10-18 12:27:26 字数 1558 浏览 3 评论 0原文

如何在不使用任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2024-10-25 12:27:26

您似乎正在尝试指定 UsersProjects 之间的多对多关系,并在关系本身上添加一个附加字段。

您当前所做的方式不是 Rails 的做事方式 - 尤其是复合主键的概念。

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

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

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

# 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-协会

It looks like you're trying to specify a many-many relationship between Users and Projects, 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 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文