Skip to content
Discussion options

You must be logged in to vote

You don't actually need PrimaryKeyConstraint.
Just specifying primary_key=True for both fields is enough to create composite primary key:

from sqlalchemy import PrimaryKeyConstraint
from sqlmodel import SQLModel, Field, create_engine


class test(SQLModel, table=True):
    src_ppn: str = Field(primary_key=True)
    dst_ppn: str = Field(primary_key=True)

    __table_args__ = (PrimaryKeyConstraint("src_ppn", "dst_ppn"),)


class test2(SQLModel, table=True):
    src_ppn: str = Field(primary_key=True)
    dst_ppn: str = Field(primary_key=True)


engine = create_engine("sqlite:///", echo=True)


SQLModel.metadata.create_all(engine)

Output (as you can see there is no difference):

CREATE TABLE …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@acidnik
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
4 participants