Charles L.
Charles L.

Reputation: 6275

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?

Upvotes: 4

Views: 2147

Answers (1)

Ilja Everilä
Ilja Everilä

Reputation: 52929

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".

Upvotes: 4

Related Questions