Python web2py引用字段unique=True属性不工作
我正在使用web2py并具有以下表定义:Python web2py引用字段unique=True属性不工作,python,mysql,web2py,Python,Mysql,Web2py,我正在使用web2py并具有以下表定义: my_info = db.define_table('my_info', Field('my_info_id', 'reference other_info', requires=IS_IN_DB(db, other_info.id, ''), unique=True), Field('interface', 'string', length=32, requires=[IS_NOT_EMPTY()]), Field('size'
my_info = db.define_table('my_info',
Field('my_info_id', 'reference other_info', requires=IS_IN_DB(db, other_info.id, ''), unique=True),
Field('interface', 'string', length=32, requires=[IS_NOT_EMPTY()]),
Field('size', 'integer', requires=[IS_NOT_EMPTY()]))
由于某些原因,当我查看mysql创建表语法时,我没有看到UNIQUE_KEY字段集。以下是创建表语法:
CREATE TABLE `mgmt_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`my_info_id` int(11) DEFAULT NULL,
`interface` varchar(32) DEFAULT NULL,
`size` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id__idx` (`id`),
CONSTRAINT `my_info_ibfk_1` FOREIGN KEY (`my_info_id`) REFERENCES `other_info` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
希望有人能让我知道为什么我不能将外键设置为unique,为什么它不起作用
谢谢,
nav这很奇怪,但更奇怪的是,在您唯一的字段中,您确实将is\u放入了\u DB验证器中。我不知道您想做什么,但您应该在数据库验证器中替换为IS_not_。因此,它将向您保证您的字段“我的信息id”在您的数据库中是唯一的。我试图使外键唯一,这在事后看来是没有意义的。这就是为什么unique在web2py中不起作用的原因,但是如果想要在纯MySQL中做到这一点,也不是不可能的。