framework icon indicating copy to clipboard operation
framework copied to clipboard

KeyError: 'table_name' when writing a resource to postgres table in schema

Open fjuniorr opened this issue 2 years ago • 0 comments

When running:

import sqlalchemy as sa
from frictionless import Resource, formats

resource = Resource(
    {
        "name": "reprex",
        "data": [
            ["name", "continent"],
            ["germany", "europe"],
            ["france", "europe"],
            ["spain", "europe"],
        ],
        "schema": {
            "fields": [
                {"name": "name", "type": "string"},
                {"name": "continent", "type": "string"},
            ]
        },
    }
)

conn_string = 'postgresql://username:password@host:port/database'

engine = sa.create_engine(conn_string)

with engine.connect() as conn:
    stm = sa.schema.CreateSchema("datapackage", if_not_exists=True)
    conn.execute(stm)
    conn.commit()

resource.write(conn_string, control = formats.SqlControl(table=resource.name, namespace='datapackage'))

I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/python3.11/site-packages/frictionless/resources/table.py", line 657, in write
    return self.write_table(target, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/python3.11/site-packages/frictionless/resources/table.py", line 451, in write_table
    parser.write_row_stream(self)
  File "/python3.11/site-packages/frictionless/formats/sql/parser.py", line 56, in write_row_stream
    adapter.write_row_stream(source.row_stream, table_name=control.table)
  File "/python3.11/site-packages/frictionless/formats/sql/adapter.py", line 149, in write_row_stream
    table = self.metadata.tables[table_name]
            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'reprex'

fjuniorr avatar Oct 17 '23 22:10 fjuniorr