Save or update for FK relationship Sqlalchemy
- by Alex
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...