-
Notifications
You must be signed in to change notification settings - Fork 19
Description
Describe the bug
The oedb_legacy generator import function fails when querying the OpenEnergyPlatform (OEP) database due to a schema naming mismatch. The generated SQL references tables in the data schema, but the remote database expects tables in the supply schema (or vice versa depending on environment configuration).
To Reproduce
Steps to reproduce the behavior:
-
Run generator import tests:
pytest tests/io/test_generators_import.py::TestGeneratorsImportOEDB::test_oedb_legacy_without_timeseries -v
-
Observe the error:
oedialect.engine.ConnectionException: HTTP 400 (Bad Request): Table data.ego_dp_conv_powerplant_nep2035_mview not found -
The issue occurs in
edisgo/io/generators_import.pyin the_import_conv_generatorsand_import_res_generatorsnested functions when executing SQL queries against the OEP database.
Expected behavior
The generator import should:
- Successfully query the OEP database regardless of which schema name the tables are stored under
- Automatically detect and use the correct schema (
datavssupply) for the current environment - Complete without HTTP 400 errors
Root Cause Analysis
-
Schema Mismatch: The SQLAlchemy ORM table definitions (from
egoio.db_tables.supply) specify tables with schemasupply(e.g.,supply.ego_dp_conv_powerplant_nep2035_mview), but the compiled SQL generates queries referencing thedataschema. -
Pandas/SQLAlchemy Compatibility: The code previously used
pd.read_sql_query(..., session.bind)which fails withAttributeError: 'Connection' object has no attribute 'exec_driver_sql'on connections that don't implement this method (common with custom dialects like oedialect). -
Naive String Replacement Fails: Attempting to fix this by simple string replacement (
sql.replace("data.", "supply.")) on the compiled SQL text causesTypeErrorin the OEP dialect's compiler when it tries to process bind parameters in a TextClause.
Additional Context
- The issue affects all
oedb_legacyimport tests in the test suite - The ORM tables are imported from
egoio.db_tables.supplyandegoio.db_tables.model_draft - The code needs to work across different database configurations (OEP remote, local egon-data, etc.)
- Investigation showed that
egoiotable objects have.fullnamereturningsupply.ego_dp_conv_powerplant_nep2035_mview, confirming the schema is defined assupplyin the ORM
Proposed Solution
Implement schema detection and reflection:
- Before executing queries, detect which schema the target tables exist under on the remote engine
- Either:
- Use SQLAlchemy's
Table(..., autoload_with=engine, schema=...)to reflect the correct table definition, or - Programmatically set the ORM table's
.schemaattribute to the detected schema before building queries
- Use SQLAlchemy's
- Use
session.execute(statement)directly instead ofpd.read_sql_queryto avoid pandas/connection compatibility issues - Convert SQLAlchemy result sets to DataFrames manually using
pd.DataFrame(result.mappings().all())
This approach will make the importer robust across different database environments without brittle string manipulation of SQL.
Related Files
edisgo/io/generators_import.py(lines ~110-230 inoedb_legacyfunction)edisgo/edisgo.py(line ~984 wheregenerators_import.oedb_legacyis called)edisgo/tools/config.py(database alias mapping configuration)
Test Cases Affected
tests/io/test_generators_import.py::TestGeneratorsImportOEDB::test_oedb_legacy_without_timeseriestests/io/test_generators_import.py::TestGeneratorsImportOEDB::test_oedb_legacy_with_worst_case_timeseriestests/io/test_generators_import.py::TestGeneratorsImportOEDB::test_oedb_legacy_with_timeseries_by_technologytests/io/test_generators_import.py::TestGeneratorsImportOEDB::test_target_capacity