1

Background

I'm working with a Mysql database generated from a legacy SQL dump file. Some tables have been deleted from said SQL dump file.

What I'm trying to do

I'm using SQLAlchemy and pymysql to select rows, and going by the official tutorial:

from sqlalchemy import create_engine, select, MetaData, Table
import pymysql

metadata = MetaData()
db_connection_str = f'mysql+pymysql://{db_user}:{db_password}@localhost/{db_name}'
db_connection = create_engine(db_connection_str)
test_table = Table(<table_name>, metadata, autoload_with=db_connection)

Works, but not for all tables. For one particular table (which exists in the database), it throws this error:

ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table '<missing_table_name>' doesn't exist")
[SQL: SHOW CREATE TABLE `<missing_table_name>`]
(Background on this error at: http://sqlalche.me/e/14/f405)

The existing table also contains a MUL Key column, with a description that says it points to <missing_table_name>. It appears that <missing_table_name> was deleted before exporting the SQL dump file.

Is there a workaround that would allow me to select rows from the existing table?

ScissorHill
  • 87
  • 1
  • 7
  • Delete the foreign key from the sql dump before importing the data to mysql? – Shadow Jan 17 '21 at 08:40
  • Or, you could create the `missing_table_name`. But it will be hard to create it with the correct (expected) definition). – Luuk Jan 17 '21 at 09:37

1 Answers1

0

I stumbled upon this somewhat-related answer which led me to wonder if Table takes a similar argument that disables foreign key checking. Per the docs, this doesn't throw the error above:

test_table = Table('<table_name>', metadata, autoload_with=db_connection,resolve_fks=False)

However, I'm not sure it'll allow me to use foreign-key based queries. Will mark this question as answered once that is confirmed.

ScissorHill
  • 87
  • 1
  • 7