Rails 中的composite_primary_key Gem 问题

发布于 2024-10-19 20:24:57 字数 1744 浏览 1 评论 0原文

我正在使用 drnic 的复合主键 gem,但遇到了问题: 如果我想在测试中使用以下命令创建 CourseOrder (具有 cpk): Course_order = CourseOrder.new(:daily_order => daily_orders(:daily_order_one_of_today_for_hans),:course => course(:non_veg_2) ), :finished => false, :ordered_at => Time.now) 我会收到以下错误:

ActiveRecord::StatementInvalid: PGError: 错误: 列“course_iddaily_order_id”中的空值违反了非空约束 :插入“course_orders”(“course_iddaily_order_id”,“course_id”,“ordered_at”,“finished”,“daily_order_id”)值(NULL,489519433,'2011-03-01 10:19:27.169397','f', 594369222)正在返回“course_iddaily_order_id”

我认为有一个course_iddaily_order_id是错误的,不是吗?

我的迁移文件如下所示:

class AddCourseOrder < ActiveRecord::Migration
  def self.up
    create_table :course_orders, :primary_key => [:course_id, :daily_order_id] do |table|
      table.integer :course_id
      table.integer :daily_order_id
      table.datetime :ordered_at, :null => false
      table.boolean :finished, :default => false, :null => false
    end
  end

  def self.down
    drop_table :course_orders
  end
end

生成的架构部分如下所示:

  create_table "course_orders", :primary_key => "course_iddaily_order_id", :force => true do |t|
    t.integer  "course_id",                         :null => false
    t.integer  "daily_order_id",                    :null => false
    t.datetime "ordered_at",                        :null => false
    t.boolean  "finished",       :default => false, :null => false
 end

我的模型如下所示:

class CourseOrder < ActiveRecord::Base
  set_primary_keys :course_id, :daily_order_id
  belongs_to :course 
  belongs_to :daily_order
end

我不知道这里出了什么问题,你能帮忙吗?

I'm using the composite primary keys gem from drnic and I've got a problem with it:
If I want to create a CourseOrder (with has a cpk) with the following command in my tests: course_order = CourseOrder.new(:daily_order => daily_orders(:daily_order_one_of_today_for_hans),:course => courses(:non_veg_2), :finished => false, :ordered_at => Time.now) I'll get the following error:

ActiveRecord::StatementInvalid: PGError: ERROR: null value in column "course_iddaily_order_id" violates not-null constraint
: INSERT INTO "course_orders" ("course_iddaily_order_id", "course_id", "ordered_at", "finished", "daily_order_id") VALUES (NULL, 489519433, '2011-03-01 10:19:27.169397', 'f', 594369222) RETURNING "course_iddaily_order_id"

I think that there is a course_iddaily_order_id is wrong, isn't it?

My Migration file looks like this:

class AddCourseOrder < ActiveRecord::Migration
  def self.up
    create_table :course_orders, :primary_key => [:course_id, :daily_order_id] do |table|
      table.integer :course_id
      table.integer :daily_order_id
      table.datetime :ordered_at, :null => false
      table.boolean :finished, :default => false, :null => false
    end
  end

  def self.down
    drop_table :course_orders
  end
end

The resulting schema part looks like this:

  create_table "course_orders", :primary_key => "course_iddaily_order_id", :force => true do |t|
    t.integer  "course_id",                         :null => false
    t.integer  "daily_order_id",                    :null => false
    t.datetime "ordered_at",                        :null => false
    t.boolean  "finished",       :default => false, :null => false
 end

My Model looks like this:

class CourseOrder < ActiveRecord::Base
  set_primary_keys :course_id, :daily_order_id
  belongs_to :course 
  belongs_to :daily_order
end

I don't know whats wrong here, can you help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

紙鸢 2024-10-26 20:24:57

我猜 create_table 方法不采用数组作为 :primary_key 选项。我想你可以省略它的设置。

I guess the create_table method doesn't takes an array for :primary_key option. I guess you can omit setting it.

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