Peewee ORM将IntegerField设置为none

时间:2016-02-29 00:17:29

标签: python peewee flask-peewee

两台服务器,结果不同。相同版本的peewee,相同版本的mysql,相同的python(2.7.10)

代码:

from peewee import *
import pprint

mysql_db = MySQLDatabase('my_database', user="root", password="")

class ItemType(Model):
    id = IntegerField()
    name = CharField()
    sort_order = IntegerField()

    class Meta:
        database = mysql_db
        db_table = 'item_type'

class SubType(Model):
    id = IntegerField()
    name = CharField()
    item_type_id = IntegerField()
    item_type = ForeignKeyField(ItemType)

    class Meta:
        database = mysql_db
        db_table = 'sub_type'


class Item(Model):
    id = IntegerField()
    name = CharField()
    hash_id = CharField()
    last_update = DateField()
    basic = BooleanField()
    sub_type_id = IntegerField()
    item_type = ForeignKeyField(ItemType)
    sub_type = ForeignKeyField(SubType)
    user_id = IntegerField()
    item_type_id = IntegerField()

    class Meta:
        database = mysql_db
        db_table = 'item'

class Attribute(Model):
    id = IntegerField()
    name = CharField()
    sort_order = IntegerField()
    basic = BooleanField()
    item_type_id = IntegerField()
    item_type = ForeignKeyField(ItemType)
    user_id = IntegerField()

    class Meta:
        database = mysql_db
        db_table = 'attribute'

class ItemAttribute(Model):
    id = IntegerField()
    value = IntegerField()
    item_id = IntegerField()
    attribute_id = IntegerField()
    item = ForeignKeyField(Item)
    attribute = ForeignKeyField(Attribute)

    class Meta:
        database = mysql_db
        db_table = 'item_attribute'

class ItemDetail(Model):
    id = IntegerField()
    small_image = CharField()
    large_image = CharField()
    description = TextField()
    item_specific_json = TextField()
    item = ForeignKeyField(Item)

    class Meta:
        database = mysql_db
        db_table = 'item_detail'

class User(Model):
    name = CharField()
    last_login = DateField()
    device_id = CharField()
    hash = CharField()
    id = IntegerField()

    class Meta:
        database = mysql_db

class UserAttribute(Model):
    id = IntegerField()
sort_order = IntegerField()
attribute = ForeignKeyField(Attribute)
user = ForeignKeyField(User)

class Meta:
    database = mysql_db
    db_table = 'user_attribute'

class UserBlockedItem(Model):
id = IntegerField()
item_id = IntegerField()
user_item_id = IntegerField()
user = ForeignKeyField(User)

class Meta:
    database = mysql_db
    db_table = 'user_blocked_item'


class UserItem(Model):
    id = IntegerField()
    default_attributes = BooleanField()
    user = ForeignKeyField(User)
    item = ForeignKeyField(Item)

    class Meta:
        database = mysql_db
        db_table = 'user_item'

class UserItemAttribute(Model):
    id = IntegerField()
    value = IntegerField()
    item = ForeignKeyField(Item)
    attribute = ForeignKeyField(Attribute)
    user = ForeignKeyField(User)

    class Meta:
        database = mysql_db
        db_table = 'user_item_attribute'

if __name__ == "__main__":
    mysql_db.connect()
    pprint.pprint(ItemAttribute.item_id)
    pprint.pprint(ItemAttribute.attribute_id)

我的mac上的输出:

< peewee.IntegerField对象位于0x10b243250>

< peewee.IntegerField对象位于0x10b243290>

我服务器上的输出

< peewee.IntegerField对象位于0x1779810>

WTF?

另一个线索。如果我改成它:

class ItemAttribute(Model):
    id = IntegerField()
    value = IntegerField()
    item_id = IntegerField()
    attribute_idx = IntegerField()
    attribute_id = IntegerField()
    item = ForeignKeyField(Item)
    attribute = ForeignKeyField(Attribute)

    class Meta:
        database = mysql_db
        db_table = 'item_attribute'

和此:

pprint.pprint(ItemAttribute.item_id)
pprint.pprint(ItemAttribute.attribute_idx)
pprint.pprint(ItemAttribute.attribute_id)

打印出来:

< peewee.IntegerField对象位于0x19d1890>
< peewee.IntegerField对象位于0x19d18d0>
< peewee.IntegerField对象位于0x19d1910>

1 个答案:

答案 0 :(得分:0)

您的冗余字段声明彼此冲突。

例如,如果你有:

attribute = ForeignKeyField(Attribute)    attribute_id = IntegerField()

这些是多余的 - 你只需要一个或另一个。如果要强制实施外键约束,请保留ForeignKeyField。如果你想只有一个整数,那就使用IntegerField。