Rails 中的composite_primary_key Gem 问题
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜
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.