Search code examples
pythonpeewee

Peewee: change "Id" field with another name


Is there a way on a Peewee model to change the default primary key named "id" into another name?


Solution

  • A couple ways...

    Auto-incrementing integer field named "pk":

    class MyModel(Model):
        pk = PrimaryKeyField()
        other_field = TextField()
    

    Varchar primary key:

    class MyModel(Model):
        data = CharField(primary_key=True)
    

    Multi-column primary key:

    class MyModel(Model):
        key = CharField()
        value = CharField()
    
        class Meta:
            primary_key = CompositeKey('key', 'value')