python - SQLAlchemy Primary Key from Unique and Primary Foreign Keys -
i have 2 tables, reports , bids, 1 many relationship report can have multiple bids. simplified report looks this:
class report(base): __tablename__ = 'reports' dr_nbr = column(char(14), primary_key=true) header_trade = column(varchar(255), unique=true) bids = relationship("biddetail", lazy="dynamic")
the bids table looks this:
class biddetail(base): __tablename__ = 'bids' report_id = column(char(14), primary_key=true) report_header_trade = column(varchar(255), primary_key=true) __table_args__ = (foreignkeyconstraint( [report_id, report_header_trade], [report.dr_nbr, report.header_trade]), {})
i have tried few ways import 2 keys reports , use primary keys. know when use primary foreign keys table need define foreignkeyconstraint
, when 1 foreign key primary , unique doesn't work. thing i've tried (by researching similar questions on so) passing 2 fields foreign keys __table_args__
uniqueconstraint
, i'm not sure i'm doing , doesn't work.
how import 2 foreign keys in sqlalchemy 1 primary key
, other has unique
constraint?
thanks @ilja-everilä answering question in comments. right way define tables is:
class report(base): __tablename__ = 'reports' dr_nbr = column(char(14), primary_key=true) header_trade = column(varchar(255)) __table_args__ = (uniqueconstraint(dr_nbr, header_trade),) contacts = relationship( 'contact', secondary='report_contact_link' ) bids = relationship("biddetail", lazy="dynamic") class biddetail(base): __tablename__ = 'bids' report_id = column(char(14), primary_key=true) report_header_trade = column(varchar(255), primary_key=true) __table_args__ = (foreignkeyconstraint( [report_id, report_header_trade], [report.dr_nbr, report.header_trade]), {})
Comments
Post a Comment