Hello,
I’m working on migrating my existing code to the updated APIs. My class
definitions for tables before looked like this:
class MyTable(Base):
__tablename__ = ‘my_table’
__table_args__ = {'autoload' : True, 'schema' : ‘my_schema’}
I’m trying to implement declarative mapping via reflected tables, as described
here:
https://docs.sqlalchemy.org/en/14/orm/declarative_tables.html#mapping-declaratively-with-reflected-tables
This is my new code:
engine = create_engine(“postgresql:…")
metadata = MetaData()
metadata.reflect(bind=dbc.engine, schema='my_schema’)
class MyTable(Base):
__table__ = Table(name=‘my_table', schema=‘my_schema',
metadata=metadata, autoload_with=engine)
This is the (truncated) error I’m getting:
AttributeError: 'Table' object has no attribute '_columns'
> /usr/custom/anaconda/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py(747)columns()
745 self._init_collections()
746 self._populate_column_collection()
--> 747 return self._columns.as_immutable()
748
749 @property
Is there more that’s needed to be done to define the class? I can create a MWE,
but thought I’d check to see if I’m missing something new.
Related to this, I currently pickle my metadata to reuse since the schema
changes infrequently. When I have this, I use:
metadata = <load from pickle>
class MyTable(Base):
__table__ = metadata.tables[‘my_schema.my_table’]
Will this still work? Although from what I read from the docs, I wondered if I
can use the Table(…) line above unchanged in this case as Metadata won’t go to
the server if the 'schema.table’ is in the class?
Cheers,
Demitri
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/027BCB9A-B700-457A-8A6A-2217C6927A0E%40gmail.com.