[英]Auto_increment custom Primary Key in Peewee model
我想要一個主鍵id
字段是Bigint
class Tweets(Model):
id = BigIntegerField(primary_key=True)
...
但它需要自動遞增,我在 Peewee 文檔中找不到方法。 如果可能,請提出建議。
更新:我正在使用 MySql 數據庫。
Peewee 自動生成一個整數id
列作為主鍵,具有 auto_increment 屬性。 這適用於您使用 Peewee 創建的任何表。
IntegerField
很可能足以滿足您的需求; BigIntegerField
很少有用。 你真的需要大於 2147483647 的數字嗎? 你會插入超過 20 億行嗎?
請參閱: http : //dev.mysql.com/doc/refman/5.5/en/integer-types.html
我認為最方便的答案是使用SQL 約束:
import peewee
class MyModel(peewee.Model):
id = peewee.BigIntegerField(primary_key=True, unique=True,
constraints=[peewee.SQL('AUTO_INCREMENT')])
Peewee,從 3.1 開始,包括一個 BigAutoField,它是一個使用 64 位整數存儲的自動遞增整數字段。 應該做的伎倆:
http://docs.peewee-orm.com/en/latest/peewee/api.html#BigAutoField
看起來這應該有幫助。
創建表后,執行:
db.register_fields({'primary_key': 'BIGINT AUTOINCREMENT'})
之后當你說
class Tweets(Model):
id = PrimaryKey()
...
class Meta():
db = db
然后在 mysql 中該字段將顯示為 BigInt 並自動遞增
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.