Ruby DataMapper - 生成null错误的串行列?

时间:2012-07-20 14:41:56

标签: sql ruby sqlite sinatra datamapper

在我构建的测试应用程序中将部分配置移动到数据库的过程中,我遇到了一个非常奇怪的问题。每当我尝试在表中创建新记录时都会生成错误。该错误表明由于ID列(模型中的DataMapper Serial)为空,SQL查询失败。在我所做的更改中,尚未修改此表的架构和用于创建新行的代码。

sinatra.error   
#<DataObjects::IntegrityError: transactions.id may not be NULL (code: 19, sql state: , query: INSERT INTO "transactions" ("uuid", "oid", "store", "txndatetime", "chargetotal") VALUES ('...', '', '...', '...', '...')

创建行的ruby语句:

txn = Transaction.create(:uuid => @uuid, :txndatetime => @timestr, :store => store, :chargetotal => params[:subtotal], :oid => params[:oid])

表格的模型:

class Transaction
  include DataMapper::Resource

  property :id,             Serial
  property :uuid,           String,   :key => true
  property :oid,            String
  property :store,          String
  property :txndatetime,    String
  property :status,         String
  property :refnumber,      String
  property :chargetotal,    String
  property :response_hash,  String,   :length => 255
  property :approval_code,  String
  property :tdate,          String

  validates_presence_of :uuid, :store
  validates_uniqueness_of :uuid
end

1 个答案:

答案 0 :(得分:1)

终于明白了。 DataMapper auto_upgrade!在sqlite db中插入了模式。丢弃表并重新生成它解决了问题。

Schema之前看起来像这样:

CREATE TABLE `transactions` (`id` integer NOT NULL, `uuid` varchar(50) NOT NULL, `oid` varchar(50), `store` varchar(50), `txndatetime` varchar(50), `status` varchar(50), `refnumber` varchar(50), `chargetotal` varchar(50), `response_hash` varchar(255), `approval_code` varchar(50), `tdate` varchar(50), `cc_num` varchar(50), `cc_expm` varchar(50), `cc_expy` varchar(50), PRIMARY KEY (`id`, `uuid`));

现在看起来像这样:

CREATE TABLE "transactions" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "uuid" VARCHAR(50) NOT NULL, "oid" VARCHAR(50), "store" VARCHAR(50), "txndatetime" VARCHAR(50), "status" VARCHAR(50), "refnumber" VARCHAR(50), "chargetotal" VARCHAR(50), "response_hash" VARCHAR(255), "approval_code" VARCHAR(50), "tdate" VARCHAR(50), "cc_num" VARCHAR(50), "cc_expm" VARCHAR(50), "cc_expy" VARCHAR(50));

缺少的AUTOINCREMENT似乎是罪魁祸首。