Peewee does not set last insert id

I am using mysql database. Peewee does not 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() 

Save 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) 

Output: id => None

The row was successfully inserted into the database. But id is still None

+4
source share
3 answers

I do not understand, but it worked when I did this:

 id = BigIntegerField(primary_key=True,index=True,unique=True , db_column="id") -> id = BigIntegerField( db_column="id") 

He also worked with these 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 does not work.

 id = BigIntegerField(primary_key=True , db_column="id") 

Maybe this is a mistake?

+1
source

This is not an error, but only PrimaryKeyField or a field with a certain sequence is considered an automatic increment.

With a bit of code, you can add a custom field type for the BigInt primary key field.

+1
source

Instead:

 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 bigint, you will need to subclass PrimaryKeyField .

+1
source

All Articles