How to transfer a Pandas dataframe to a database matching foreign keys using pd.to_sql and SQLAlchemy functionality?

Viewed 71

I want to append data to a database table that has foreign keys linking to another table but I haven’t found a way to use SQLAlchemy functionality to match the existing foreign keys.

Let’s say I have an actor table that automatically generates its primary keys and a movies table that links to the actor id with a foreign key (one to many relationship):

class Actor(Base):
    __tablename__ = 'actors'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    def __init__(self, name, birthday):
        self.name = name

class Movie(Base):
    __tablename__ = 'movies'
    id = Column(Integer, primary_key=True)
    title = Column(String)
    actor_id = Column(Integer, ForeignKey('actors.id'))
    actor = relationship("Actor", backref="movies")
    def __init__(self, title, release_date):
        self.title = title
        self.actor = actor

I want to add a dataframe with movie titles to the “movies” table, for actors that already exist in the “actors” table.

movies_df = pd.DataFrame({'title': ['bourne_identity', 'furious_7'], 'actor':['matt_damon', 'dwayne_johnson']})

I could read the “actors” table, retrieve the ids for the actors and generate a dataframe that could easily load to the database:

movies_df_id = pd.DataFrame({'title': ['bourne_identity', 'furious_7'], 'actor_id':[0, 1]})
movies_df_id.to_sql('movies',con=engine, if_exists='append',index=False)

Is there a way to use the SQLAlchemy ORM functionality to achieve this and just pass the actor names for each movie to the database without having to retrieve and match their ids first?

0 Answers
Related