简体   繁体   中英

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. 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.

I also tried few workarounds like giving default='' but did not help. Here is another example with MySQL backend

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'. Anybody has any idea ? How to make web2py to add 'NOT NULL' for foreign keys ?

Note: It does not make any difference if default='' is removed. I added it as per @simplyharsh suggestions and discussion here http://www.mail-archive.com/web2py@googlegroups.com/msg12879.html

When notnull=True is set, you should also set a default attribute.

Consider this thread .

notnull=True in the context of web2pys DAL means:

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).

To force the web2py user to insert a value, you need to define the required widget of the field. 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. Have you tried something along the lines of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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