ponyorm and python: Postgresql Cannot use composite key when one of the key is auto increment i.e serial or identity

236 Views Asked by At

When using pony orm, if primary key is a composite key and one of the key contains postgresql identity column then pony ORM shows error

There is a good reason for me to use a composite key as a primary key instead of just the identity column, and it has to be a composite key

Errors

None type Object not iterable
If obj._pk_is_composite_: pairs = izip(pk_attrs, pkval)

MOdel

id = Required(int, auto=True) 
clubname = Required(str)
PrimaryKey(clubname,id)

Table

id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
clubname character varying(128),
CONSTRAINT abc PRIMARY KEY(clubname,id)
1

There are 1 best solutions below

0
Belayer On

There is no good for having a composite Primary Key with one component is auto generated. I think what you are trying to say is that clubname should be unique, if so your composite key does not accomplish that as (clubname,id) values ('A',1) and ('A',2) satisfy your composite key. Instead just create a unique constraint on clubname itself.

create table some_table(
             id         integer generated always as identity
           , clubname   character varying(128) not null,
           , constraint some_table_pk primary key(id) 
           , constraint some_table_bk unique (clubname)
           );