I am new to sqlalchemy. I followed the tutorial to create an automap of an existing db with a relation from mysql db
from sqlalchemy import create_engine, MetaData, Column, Table, ForeignKey from sqlalchemy.ext.automap import automap_base, generate_relationship from sqlalchemy.orm import relationship, backref from config import constr, mytables def _gen_relationship(base, direction, return_fn, attrname, local_cls, refferred_cls, **kw): return generate_relationship(base, direction, return_fn, attrname, local_cls, refferred_cls, **kw) engine = create_engine(constr) metadata = MetaData() metadata.reflect(engine, only=mytables) Base = automap_base(metadata=metadata) Base.prepare(engine, reflect=True, generate_relationship=_gen_relationship) Tableclass1 = Base.classes.table1 Tableclass2 = Base.classes.table2
Table2.ID mapped to one of the columns of table1 . But when I tried to use the query and joined table1 and table2 , it reports an error saying that "Cannot find the relationship with the foreign key." Since I know the relationship of these two tables, is there any way for me to declare this relationship after creating the class instance? Or is there a way to explicitly talk about this relationship in the query function? Thanks!
python mysql sqlalchemy relationship automap
capaneus
source share