I use mysql database. Peewee doesn't set id
field after save()
Peewee Model :
class OAuthAccount(BaseModel):
id = BigIntegerField(primary_key=True,index=True,unique=True , db_column="id")
oauth_provider_id = IntegerField(null=False)
oauth_uid = CharField()
oauth_token = CharField()
oauth_token_secret = CharField()
username = CharField()
inserter = BigIntegerField(null=True,db_column="inserter_id")
insert_date = DateTimeField(null=True,default=fn.NOW())
updater = BigIntegerField(null=True,db_column="updater_id")
update_date = DateTimeField(null=True)
extra_data = CharField()
The saving code :
oauthUser = OAuthAccount(
oauth_provider_id= formData.get("oauth_provider_id"),
oauth_uid = formData.get("oauth_uid"),
oauth_token = formData.get("oauth_token"),
oauth_token_secret=formData.get("oauth_token_secret"),
username = formData.get("username"),
inserter_id = g.auth.userId(),
extra_data = formData.get("extra_data")
)
oauthUser.save()
print "id => " + str(oauthUser.id)
The output is : id => None
The row successfully inserted to database. But id
is still None
I don't understand but it worked when i did this changes:
id = BigIntegerField(primary_key=True,index=True,unique=True , db_column="id")
->
id = BigIntegerField( db_column="id")
It alse worked with this changes:
id = BigIntegerField(primary_key=True,index=True,unique=True , db_column="id")
->
id = PrimaryKeyField( db_column="id")
When I put primary_key=True
it doesn't work.
id = BigIntegerField(primary_key=True , db_column="id")
Maybe it is a bug?
This isn't a bug, per-se, but only the PrimaryKeyField
or a field with a sequence defined is considered to be auto-incrementing.
With a bit of code, you could add a custom field type for BigInt primary key field, though.
Instead of:
class OAuthAccount(BaseModel):
id = BigIntegerField(primary_key=True,index=True,unique=True , db_column="id")
Try:
class OAuthAccount(BaseModel):
id = PrimaryKeyField()
Then the id
field will be set on your model after save
and create
.
If you need to use a bigint you'll need to subclass PrimaryKeyField
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.