the constructor for Table is not expecting that you pass the "name" and 
"metadata" argument as keyword arguments and this is causing the reflection 
process to not occur at all.

specify the table like this:

Table('my_table', metadata, schema='test_schema', autoload_with=engine) 

There's some unsmoothness here in SQLAlchemy both not warning about the keyword 
arguments and being ungraceful when no columns are present so 
https://github.com/sqlalchemy/sqlalchemy/issues/6135 is added.




On Thu, Mar 25, 2021, at 4:56 PM, demitr...@gmail.com 
<mailto:demitr...%40gmail.com> wrote:
> Hi Mike,
> 
> Thanks for the reply. Below is a minimal complete example on SQLAlchemy 
> v1.4.2, Python 3.8.3.
> 
> PostgreSQL schema:
> 
> CREATE SCHEMA autoload_test AUTHORIZATION postgres;
> 
> ALTER DEFAULT PRIVILEGES IN SCHEMA autoload_test GRANT ALL ON TABLES TO 
> demitri;
> 
> CREATE TABLE autoload_test.my_table
> (
>     pk integer NOT NULL,
>     label text COLLATE pg_catalog."default" NOT NULL,
>     CONSTRAINT my_table_pkey PRIMARY KEY (pk)
> );
> 
> ALTER TABLE autoload_test.my_table OWNER to postgres;
>     
> Python script:
> 
> #!/usr/bin/env python
> 
> from sqlalchemy.orm import registry
> from sqlalchemy.schema import Table
> from sqlalchemy import create_engine, MetaData
> 
> engine = create_engine("postgresql://postgres@localhost:5432/postgres")
> metadata = MetaData()
> metadata.reflect(bind=engine, schema='autoload_test')
> 
> mapper_registry = registry()
> Base = mapper_registry.generate_base()
> 
> class MyTable(Base):
>     __table__ = Table(name='my_table', schema='autoload_test',
>                       metadata=metadata, autoload_with=engine)
> 
> 
> I'm also curious how to accomplish the same with a @mapper_registry.mapped 
> decorator, but first things first. :)
> 
> And this is the traceback I get:
> 
> Traceback (most recent call last):
>   File "/Users/demitri/autoload_mve/autoload_test.py", line 14, in <module>
>     class MyTable(Base):
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/decl_api.py", 
> line 75, in __init__
>     _as_declarative(reg, cls, dict_)
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/decl_base.py",
>  line 126, in _as_declarative
>     return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/decl_base.py",
>  line 177, in setup_mapping
>     return cfg_cls(registry, cls_, dict_, table, mapper_kw)
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/decl_base.py",
>  line 314, in __init__
>     self._early_mapping(mapper_kw)
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/decl_base.py",
>  line 200, in _early_mapping
>     self.map(mapper_kw)
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/decl_base.py",
>  line 971, in map
>     mapper_cls(self.cls, self.local_table, **self.mapper_args),
>   File "<string>", line 2, in __init__
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py",
>  line 298, in warned
>     return fn(*args, **kwargs)
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", 
> line 684, in __init__
>     self._configure_properties()
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", 
> line 1421, in _configure_properties
>     for column in self.persist_selectable.columns:
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py",
>  line 1095, in __get__
>     obj.__dict__[self.__name__] = result = self.fget(obj)
>   File 
> "/usr/local/anaconda/lib/python3.8/site-packages/sqlalchemy/sql/selectable.py",
>  line 747, in columns
>     return self._columns.as_immutable()
> AttributeError: 'Table' object has no attribute '_columns'
> 
> 
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/bf439736-6195-449b-8256-5afd355e7161n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/bf439736-6195-449b-8256-5afd355e7161n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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/cdd4b5e2-032c-4e45-852b-f99335404dd4%40www.fastmail.com.

Reply via email to