shtuper
shtuper

Reputation: 3916

Auto_increment custom Primary Key in Peewee model

I want a primary key id field to be Bigint

class Tweets(Model):
    id = BigIntegerField(primary_key=True)
    ...

But it needs to be auto_incremented and I can't find a way in the Peewee docs. Please suggest if it's possible.

Update: I'm using MySql db.

Upvotes: 5

Views: 10241

Answers (4)

Yurii Rabeshko
Yurii Rabeshko

Reputation: 631

I think the most convenience answer is by using SQL constraints:

import peewee

class MyModel(peewee.Model):
    id = peewee.BigIntegerField(primary_key=True, unique=True,
            constraints=[peewee.SQL('AUTO_INCREMENT')])

Upvotes: 2

coleifer
coleifer

Reputation: 26235

Peewee, as of 3.1, includes a BigAutoField which is an auto-incrementing integer field using 64-bit integer storage. Should do the trick:

http://docs.peewee-orm.com/en/latest/peewee/api.html#BigAutoField

Upvotes: 3

akaRem
akaRem

Reputation: 7618

Looks like this should help.

After creating table, do:

db.register_fields({'primary_key': 'BIGINT AUTOINCREMENT'})

After that when you say

class Tweets(Model):
    id = PrimaryKey()
    ...
    class Meta():
        db = db

Then in mysql that field will appear as BigInt with auto increment

Upvotes: 1

Mathieu Rodic
Mathieu Rodic

Reputation: 6762

Peewee automatically generates an integer id column serving as primary key, having the auto_increment property. This is true for any table you create with Peewee.

It is very likely that IntegerField is enough for your needs; BigIntegerField is very rarely useful. Will you really need numbers bigger than 2147483647? Will you insert more than two billion rows?

See: http://dev.mysql.com/doc/refman/5.5/en/integer-types.html

Upvotes: 4

Related Questions