Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy Many-to-one unidirectional relationship

I am working with SQLAlchemy and I have a table I want to add, order, which references a currency table.

I want a foreign key relationship setup so the order points to a currency in the currency table, but I don't want the currency table to contain a list of orders which use that currency. Most of the docs make me think that relationships go two ways, but I want something that is more one directional, that match raw SQL like:

CREATE TABLE order ... FOREIGN KEY(currency_id) REFERENCES currencies(id)

Is this possible with SQLAlchemy?

like image 788
Charles L. Avatar asked Oct 19 '25 04:10

Charles L.


1 Answers

Simply don't define a relationship on the other end, in your case "currency", and don't use backref. Relationships are actually unidirectional by default and you have to use back_populates etc. in order to make them bidirectional. This is covered in "Basic Relationship Patterns".

like image 90
Ilja Everilä Avatar answered Oct 20 '25 19:10

Ilja Everilä



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!