Composite key; one field is also foreign key

7 views
Skip to first unread message

Rich Shepard

unread,
Jun 18, 2019, 4:13:02 PM6/18/19
to sqlal...@googlegroups.com
I understand that multiple fields in a declarative base class can be
specified as a composite primary key. In my application rather than using a
unique, serialized primary key for each table/field I use a foreign key
reference to a row in the locations table and a date. For example,

site_name = Column(Integer, ForeignKey('locations.site_name', \
onupdate='CASCADE', ondelete='RESTRICT'))
sampdate = Column(Date, nullable=False)

How do I specify both as a composite primary key?

TIA,

Rich


Jonathan Vanasco

unread,
Jun 18, 2019, 5:53:34 PM6/18/19
to sqlalchemy
adding `primary_key=True` to each of the `Column` constructors should do it.




Rich Shepard

unread,
Jun 18, 2019, 6:05:26 PM6/18/19
to sqlalchemy
On Tue, 18 Jun 2019, Jonathan Vanasco wrote:

> adding `primary_key=True` to each of the `Column` constructors should do it.

Thanks, Jonathan.

Rich
Reply all
Reply to author
Forward
0 new messages