hi there -

would need a complete example and SQLAlchemy version in use, Table does have a 
"_columns" attribute and both patterns described below are things that should 
work without an error like this, additionally the complete stack trace would 
also be helpful.     Unpickling the metadata is a place where it is more 
plausible that the state of the Table is broken but that does not apply when 
you are just doing autoload_with.

On Thu, Mar 25, 2021, at 1:33 PM, Demitri Muna wrote:
> 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 sqlalchemy+unsubscr...@googlegroups.com 
> <mailto:sqlalchemy%2Bunsubscribe%40googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/027BCB9A-B700-457A-8A6A-2217C6927A0E%40gmail.com.
> 
 

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/abbbd7c7-b12d-4e40-95cd-f1147c432f5e%40www.fastmail.com.

Reply via email to