2013-04-17 35 views
2

我有以下腳本:插入到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(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; 

預先感謝您!

+0

是否更改了表的創建後,你的模型的領域? –

+0

丹尼斯,不是我沒有,我剛剛開始學習peewee,上面的代碼幾乎是我的一切。 – Kenuat

+0

以及你是如何創建這張桌子的? –

回答