简体   繁体   English

如何使DataMapper不指定NULL默认值

[英]How to get DataMapper to not specify a NULL default value

I'm getting an error when I use DataMapper's auto_upgrade! 使用DataMapper的auto_upgrade!时出现错误auto_upgrade! method to add fields in an SQLite3 db based on the properties defined in my code: 根据我的代码中定义的属性在SQLite3数据库中添加字段的方法:

DataObjects::SyntaxError at /history / history处的DataObjects :: SyntaxError
Cannot add a NOT NULL column with default value NULL 无法添加默认值NULL的NOT NULL列

An example of an offending line would be: 违规行的示例为:

property :fieldname, Text,  required: true

The error goes away if I (a) remove the line, (b) remove required: true , (c) change true to false , or (d) add a default value. 如果我(a)删除该行,(b)删除required: true ,(c)将true更改为false ,或(d)添加默认值,则错误消失。

SQLite does not require a default value to be specified for every field, so this problem is definitely with DataMapper, not SQLite. SQLite 要求每个字段中指定一个默认值,所以这个问题肯定是DataMapper的,而不是SQLite的。

How can I get around this, so DataMapper can specify that a field is required without assuming that not specifying a default value automatically means the default should be NULL? 如何解决这个问题,以便DataMapper可以指定一个必填字段,而无需假设不自动指定默认值就意味着默认值应该为NULL?

(If you want to know more about why I'm designing this way: there will be another client process accessing SQLite and logging data into the SQLite database, while a Sinatra app will be pulling data out of the db for display in a browser. I want the database therefore to enforce the field requirements, but DM's auto_upgrade is a very convenient way to be able to upgrade the database as needed—so long as it doesn't foul things up in the process.) (如果您想更多地了解为什么我要用这种方式进行设计:将有另一个客户端进程访问SQLite并将数据记录到SQLite数据库中,而Sinatra应用程序将从数据库中提取数据以在浏览器中显示。因此,我希望数据库能够满足现场要求,但是DM的auto_upgrade是一种非常方便的方法,能够根据需要升级数据库-只要它不会在过程中造成问题。)

You are requiring the field, hence it cannot be NULL. 您需要该字段,因此它不能为NULL。 This is simple table properties. 这是简单的表属性。

When DataMapper runs auto_upgrade! 当DataMapper运行auto_upgrade时! it runs the SQL commands on the database. 它在数据库上运行SQL命令。

CREATE TABLE Test
(
  P_Id int NOT NULL,
  lname varchar(255) NOT NULL,
  fname varchar(255),
  Address varchar(255),
  City varchar(255)
)

And doing something like this won't work. 而且做这样的事情是行不通的。

CREATE TABLE Test
(
  P_Id int NOT NULL,
  lname varchar(255) NOT NULL DEFAULT NULL,
  fname varchar(255),
  Address varchar(255),
  City varchar(255)
)

I tested it in MySQL and this is the error. 我在MySQL中测试了它,这是错误。

02:52:43 CREATE TABLE TestTest ( P_Id int NOT NULL, lname varchar(255) NOT NULL DEFAULT NULL, fname varchar(255), Address varchar(255), City varchar(255) ) Error Code: 1067. Invalid default value for 'lname' 0.062 sec 02:52:43 CREATE TABLE TestTest(P_Id int NOT NULL,lname varchar(255)NOT NULL DEFAULT NULL,fname varchar(255),Address varchar(255),City varchar(255))错误代码:1067.无效的默认值持续0.062秒

Correction: SQLite does allow you to create a table with such properties. 更正:SQLite允许您创建具有此类属性的表。 However, when trying to insert anything to that table makes it throw an error whether the field is NULL or not. 但是,在尝试向该表中插入任何内容时,无论该字段是否为NULL,都会引发错误。 So DataMapper might be doing some sanitation for your before even creating the table. 因此,DataMapper可能甚至在创建表之前都会为您做一些清洁工作。

It is not clear to me if you are creating a new table or modifying an existing one. 对我来说不清楚是要创建新表还是修改现有表。

If you have an existing table and are trying to alter it with a column defined as NOT NULL, then you must provide a default value so that the existing rows can be migrated. 如果您有一个现有表,并尝试使用定义为NOT NULL的列对其进行更改,则必须提供一个默认值,以便可以迁移现有行。 The RDBMS needs to know what to put in the field for pre-existing rows. RDBMS需要知道要在字段中放置哪些行。

If you are creating a new table, then the property definition you have should be fine. 如果要创建新表,则可以使用属性定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM