2
我在一个简单的应用程序中使用DataMapper来跟踪销售。我有一个Day
类,像这样:Datamapper - 奇怪的可能不是NULL错误
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
d,但这不能解决错误。
我破了什么?
编辑的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");
错误的语法看起来像它可能来自数据库。你可以进入数据库控制台并检查模式(在此处发布)? –
我该怎么做?我忘了提及,数据库是SQLite3。 – Nekkoru
只需在控制台输入'sqlite3',然后在'.help'中输入'.schema sales'来获得帮助,'.quit'退出。如果这是生产数据,请避免做其他事情(虽然您可以将该文件的副本用于工作) –