forcing (composite) primary key order?

12 views
Skip to first unread message

Jonathan Vanasco

unread,
Jun 1, 2020, 6:16:58 PM6/1/20
to sqlalchemy
is it possible to force the order of primary keys?

for example in this setup...

class AcmeDnsServer2Domain(Base):
    __tablename__
= "acme_dns_server_2_domain"
    acme_dns_server_id
= sa.Column(
        sa
.Integer, sa.ForeignKey("acme_dns_server.id"), primary_key=True
   
)
    domain_id
= sa.Column(
        sa
.Integer, sa.ForeignKey("domain.id"), primary_key=True
   
)



I want to influence the creation of the primary key to be ["acme_dns_server_id", "domain_id"]

In several projects I've worked on, the order has been important.  (it can affect query planning and caching)

I thought I had seen something in the docs several years ago, but I just spend a few fruitless minutes trying to find it.

Mike Bayer

unread,
Jun 1, 2020, 7:15:23 PM6/1/20
to noreply-spamdigest via sqlalchemy
yes use the PrimaryKeyConstraint() construct



here you'd want to put it in your __table_args__ and remove primary_key=True from each column
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Jonathan Vanasco

unread,
Jun 2, 2020, 2:17:24 PM6/2/20
to sqlalchemy
thanks mike!
Reply all
Reply to author
Forward
0 new messages