Save or update for FK relationship Sqlalchemy
Posted
by Alex
on Stack Overflow
See other posts from Stack Overflow
or by Alex
Published on 2010-02-27T10:01:36Z
Indexed on
2010/03/09
11:36 UTC
Read the original article
Hit count: 188
sqlalchemy
|foreign-keys
I've googled, but haven't been able to find the answer to this seemingly simple question.
I have two relations, a customer and an order. Each order is associated to a single cusomter, and therefore has a FK relationship to the customer table. The customer relation only stores customer names, and I have set a unique constraint on the customer table barring duplicate names.
Let's say I create a new order instance and set a customer for the order. Something like:
order_instance.customer = Customer("customer name")
When I save the order instance, SqlAlchemy will complain if a customer with this name already exists in the customer table.
How do I specify to SqlAlchemy to insert into the customer table if a customer with this name doesn't already exist, or just ignore (or even update) to the customer relation? I don't really want to have to check each time if a customer with some name already exists...
© Stack Overflow or respective owner