Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python peewee not null约束失败,将_id添加到类_Python_Python 3.x_Sqlite_Peewee - Fatal编程技术网

Python peewee not null约束失败,将_id添加到类

Python peewee not null约束失败,将_id添加到类,python,python-3.x,sqlite,peewee,Python,Python 3.x,Sqlite,Peewee,以下是我试图在peewee/python中实现的模型: class Person(BaseModel): name = peewee.Charfield(unique=True) rate = peewee.FloatField() class Unit(BaseModel): person = peewee.ForeignKeyField(Person) 我创建表,然后尝试在数据库中创建一些虚拟数据: from above import Person, Unit

以下是我试图在peewee/python中实现的模型:

class Person(BaseModel):
    name = peewee.Charfield(unique=True)
    rate = peewee.FloatField()

class Unit(BaseModel):
    person = peewee.ForeignKeyField(Person)
我创建表,然后尝试在数据库中创建一些虚拟数据:

 from above import Person, Unit

 ben = Person()
 ben.name = "Ben Dover"
 ben.rate = 21.5
 ben.save()

 hour_one = Unit()
 hour_one.person = ben
 hour_one.save()
当我运行这两个程序时,第二个程序在
hour\u one.save()
行出现错误,错误如下:

peewee.IntegrityError: NOT NULL constraint failed: unit.person_id
我不知道为什么peewee认为person上有person\u id属性。我认为person.name将是该表中唯一的标识列。我尝试将
unique=True
替换为
primary\u key=True
,并尝试将两者都包括在内,但错误依然存在


如果我没有提供
ben.name
,我可以理解,但我做到了。那么peewee遗漏了哪些信息,它认为是空的?

您能给我们展示一下您的表结构吗?@giorgosmyriantous没有现有的表。定义完模型后,我用
create_tables([Person,Unit])
创建模型,尝试以下操作:
hour_one=Unit(Person=ben)hour_one.save()
@giorgosmyriantous,它会引发相同的错误。是的。在创建_表()之前使用drop_tables()。