Datamapper - 奇怪的可能不是NULL错误

时间:2013-06-21 11:17:04

标签: ruby datamapper

我在一个简单的应用程序中使用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.nowsave d了,但这并不能解决错误。< / p>

我打破了什么?

编辑 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");

1 个答案:

答案 0 :(得分:2)

我想我修好了。显然,我date中的某个点有一个旧的Sale属性。我进入ruby解释器,需要我的模型并使用DataMapper.auto_migrate!重置整个数据库。这解决了这个问题。