简体   繁体   English

如何在Web2py中的外键上添加NOT NULL约束

[英]How to add NOT NULL constraint on foreign keys in Web2py

I am having trouble in making my models to generate foreign keys as not null in Web2py. 我在使模型生成Web2py中不为null的外键时遇到麻烦。 I have tried everything that I knew and all that I could find on the web. 我已经尝试了所有我知道的东西以及可以在网上找到的所有东西。 Here is a simple example: 这是一个简单的示例:

db = DAL('sqlite://storage.db')
users=db.define_table('user', Field('name') )
cars=db.define_table('cars', Field('user', users, notnull=True), Field('Model') )
print db._lastsql

This print ===
CREATE TABLE cars(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user INTEGER REFERENCES users(id) ON DELETE CASCADE,
    Model CHAR(512)
);
=============

Looks like web2py ignored notnull=True for some reason. 看起来由于某种原因,web2py忽略了notnull = True。

I also tried few workarounds like giving default='' but did not help. 我也尝试了一些变通办法,例如给default ='',但没有帮助。 Here is another example with MySQL backend 这是MySQL后端的另一个示例

db = DAL('mysql://test:test@localhost:3306/test')
db.define_table('property',
   Field('type', notnull=True),
   Field('area','integer', notnull=True),
   Field('rooms','integer', default = 0, notnull=True))

db.define_table('media',
   Field('title', length = 30),
   Field('file', 'upload', autodelete=True, notnull=True),
   Field('prop_id', db.property, notnull=True, default='', required=True))

CREATE TABLE SQL:
CREATE TABLE  `propdb`.`media` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(30) DEFAULT NULL,
  `file` varchar(512) NOT NULL,
  `prop_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `prop_id__idx` (`prop_id`),
  CONSTRAINT `prop_media_ibfk_1` FOREIGN KEY (`prop_id`) REFERENCES `prop_property` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

In MySQL it not only ignored notnull but made the column 'DEFAULT NULL' as you can see column 'prop_id'. 在MySQL中,它不仅忽略了notnull,而且使列'DEFAULT NULL'变为了,正如您所看到的'prop_id'列一样。 Anybody has any idea ? 有人有什么主意吗? How to make web2py to add 'NOT NULL' for foreign keys ? 如何使web2py为外键添加“ NOT NULL”?

Note: It does not make any difference if default='' is removed. 注意:如果删除default ='',则没有任何区别。 I added it as per @simplyharsh suggestions and discussion here http://www.mail-archive.com/web2py@googlegroups.com/msg12879.html 我根据@simplyharsh的建议和讨论在此处http://www.mail-archive.com/web2py@googlegroups.com/msg12879.html上添加了它

When notnull=True is set, you should also set a default attribute. 设置notnull=True ,还应该设置默认属性。

Consider this thread . 考虑一下这个线程

notnull=True in the context of web2pys DAL means: notnull =在web2pys DAL上下文中为True表示:

The content of this field never be 'null' (false), the consequence is, that selects for string colums will return an empty string, integer or float columns will return the value 0 instead of NULL (DAL: None or false). 该字段的内容永远不会为'null' (假),结果是,选择字符串列将返回空字符串,整数或浮点列将返回值0而不是NULL(DAL:无或假)。

To force the web2py user to insert a value, you need to define the required widget of the field. 要强制web2py用户插入值,您需要定义该字段的必需小部件。 For example: 例如:

db.tablefoo.fieldbar.requires = IS_IN_SET('OK', 'NOT OK')

I am assuming you don't want the foreign key to be null, because it should correspond to something that exists already. 我假设您不希望外键为null,因为它应该对应于已经存在的东西。 Have you tried something along the lines of 您是否尝试过以下方法

db.property.area.requires=IS_IN_SET([Insert your list/array of possibilities])

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

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