tito swineflu
tito swineflu

Reputation: 51

Peewee ORM sets IntegerField to none

Two servers, different results. Same version of peewee, same version of mysql, same python (2.7.10)

code:

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)

Output on my mac:

<peewee.IntegerField object at 0x10b243250>

<peewee.IntegerField object at 0x10b243290>

output on my server

<peewee.IntegerField object at 0x1779810>

None

WTF?

Another clue. If I change it to this:

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'

and this:

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

it prints out this:

<peewee.IntegerField object at 0x19d1890>
<peewee.IntegerField object at 0x19d18d0>
<peewee.IntegerField object at 0x19d1910>

Upvotes: 1

Views: 279

Answers (1)

coleifer
coleifer

Reputation: 26245

You have redundant field declarations that are conflicting with one another.

For instance, if you have:

attribute = ForeignKeyField(Attribute) attribute_id = IntegerField()

These are redundant -- you only need one or the other. If you want to enforce a foreign key constraint, keep the ForeignKeyField. If you want to just have an integer, then use the IntegerField.

Upvotes: 1

Related Questions