I am using an existing mysql database which has pre existing tables. Everything works as expected when I manually create classes/ models using the declarative approach. Now, I want to use automap to avoid creating models manually. As the database is from work, I cannot edit it. Second, the primary keys are present already(Most posts I have read mention primary keys not being present on the table as the reason).
I have been looking around for a couple of days but have not found a solution.
Example below :
from sqlalchemy.ext.automap import automap_base
from sqlalchemy import create_engine
Base = automap_base()
engine = create_engine('working-as-expected')
Base.prepare(autoload_with=engine)
print('Hello2 - base classes items')
print(Base.classes.items()) # output is []
print('Hello3 - metadata keys')
print(Base.metadata.tables.keys()) # output is []
example of an existing mysql table :
CREATE TABLE 'tbl_Xyz' (
'ID' int(11) NOT NULL,
'Name' varchar(45) NOT NULL,
PRIMARY KEY ('ID'))
What should I do differently ?
This is working for me, do you think you might not be referencing the correct database name? I used the
mysql:8docker container to run mysql.In addition to all the logging from
echo=Trueon the engine this script prints this:[('tbl_Xyz',)]