Python 使用peewee插入MySQL表会引发;“未知列”;例外
我有以下脚本:Python 使用peewee插入MySQL表会引发;“未知列”;例外,python,mysql,peewee,Python,Mysql,Peewee,我有以下脚本: from peewee import * db = MySQLDatabase('database', user='root') class BaseModel(Model): class Meta: database = db class Locations(BaseModel): location_id = PrimaryKeyField() location_name = CharField() class Units(Bas
from peewee import *
db = MySQLDatabase('database', user='root')
class BaseModel(Model):
class Meta:
database = db
class Locations(BaseModel):
location_id = PrimaryKeyField()
location_name = CharField()
class Units(BaseModel):
unit_id = PrimaryKeyField()
unit_num = IntegerField()
location_id = ForeignKeyField(Locations, related_name='units')
db.connect()
for location in Locations.select():
for pod_num in range (1, 9):
unit = Units.create(unit_num=pod_num, location_id=location.location_id)
表位置的行数较少,表单位为空。当我尝试启动它时,我不断遇到异常:
(1054, "Unknown column 'location_id_id' in 'field list'")
我做错了什么
以下是用于创建表的SQL脚本的一部分:
CREATE TABLE IF NOT EXISTS `database`.`units` (
`unit_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`unit_num` TINYINT UNSIGNED NOT NULL ,
`location_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`unit_id`) ,
UNIQUE INDEX `ID_UNIQUE` (`unit_id` ASC) ,
INDEX `location_idx` (`location_id` ASC) ,
CONSTRAINT `location_id`
FOREIGN KEY (`location_id` )
REFERENCES `database`.`locations` (`location_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
提前谢谢你 如果要显式指定列,请使用
db\u column
:
class Units(BaseModel):
unit_id = PrimaryKeyField()
unit_num = IntegerField()
location_id = ForeignKeyField(Locations, db_column='location_id', related_name='units')
这是有据可查的:在创建表之后您是否更改了模型的字段?Denis,不是我没有,我刚刚开始学习peewee,上面的代码几乎是我所有的。您是如何创建此表的?使用上面的SQL脚本(我使用Workbench,所以它是EER模型->导出脚本->运行脚本)。刚刚发现将列“location\u id”重命名为“location”解决了问题:),但我仍然希望使用“location\u id”名称(因为我不允许在生产数据库中更改其名称),所以问题仍然是实际的