I'm using sqlalchemy to insert data into a table. The things I care about:
- I expect to be inserting a lot of data so want minimize the number of database execution.
- There will be duplicate entries, this is expected, so I'll use
on_conflict_do_nothing(I'm using postgres).
As a minimal example, let's say I have two tables:
from sqlalchemy import Column, Integer, ForeignKey, String
Base = declarative_base()
class Address(Base):
__tablename__ = "addresses"
id = Column(Integer, primary_key=True)
street = Column(String)
number = Column(Integer)
class Person(Base)
__tablename__ = "people"
id = Column(Integer, primary_key=True)
name = Column(String)
address_id = Column(Integer, ForeignKey("addresses.id")
address = relationship("Address")
I can batch up all the Address inserts easily enough with:
from sqlalchemy.dialects.postgresql import insert
values = [{"street": "main", "number": 1}, {"street": "main", "number": 2}]
statement = insert(Address).values(values).on_conflict_do_nothing()
session.execute(statement)
The question is how do I then do the same with the People? I'm struggling with what to supply for "address", e.g.
values = [{"name": "John", "address": ????}]
statement = insert(Person).values(values).on_conflict_do_nothing()
session.execute(statement)
I'm assuming I need the Address object but then I don't know where to go from there?