我在一个简单的应用程序中使用 DataMapper 来跟踪销售。我有一Day
堂课,像这样:
class Day
include DataMapper::Resource
property :id, Serial, :key => true
property :date, DateTime
property :bestseller, String
property :total_money, Decimal
property :total_sold, Integer
property :total_orders, Integer
has n, :sales
end
和一个Sales
班级:
class Sale
include DataMapper::Resource
belongs_to :day
property :id, Serial, :key => true
property :name, String
property :amount, Integer
property :value, Integer
end
尝试向Sale
数据库添加新的时,如下所示:
s = Sale.new(:day => Day.get(1), :name => "Foo", :amount => "42", :value => "42"
调用时出现此错误save
。
DataObjects::IntegrityError at /sell
sales.date may not be NULL
我在 中没有date
财产Sale
,所以我不确定这是从哪里来的。首先我认为Day
我得到的对象没有设置日期,所以我做了d = Day.get(1).date = Time.now
并做save
了它,但这并不能解决错误。
我打破了什么?
编辑sqlite3 模式
CREATE TABLE "sales" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" VARCHAR(50),
"amount" INTEGER,
"value" INTEGER,
"day_id" INTEGER NOT NULL,
"drink_id" INTEGER NOT NULL
);
CREATE INDEX "index_sales_day" ON "sales" ("day_id");
CREATE INDEX "index_sales_drink" ON "sales" ("drink_id");