On Fri, Jul 29, 2022, at 12:16 AM, jens.t...@gmail.com wrote: > After digging through docs and some more code, here’s what seems to work. > > The single, shared package should not generate that *Base* class which is > then used by other packages. Instead, it now provides a function that > generates and returns a *Base* class; better yet, that function takes a > *schema > <https://docs.sqlalchemy.org/en/14/core/metadata.html#sqlalchemy.schema.MetaData.params.schema>* > parameter and passes that on to the MetaData initializer. > > With that function available, all other packages can create their own *Base* > class and use that for their respective table mappings. Passing the schema > parameter also allows a small step towards customizing that generated Base, > e.g. with a schema name for PostgreSQL > <https://www.postgresql.org/docs/current/ddl-schemas.html> dbs. > > I think I can now create an Engine and Connection > <https://docs.sqlalchemy.org/en/14/core/connections.html>, and use the > different *Base* classes and their mappings in isolation… > > One interesting detail: SQLA defines the type returned by *declarative_base() > <https://docs.sqlalchemy.org/en/14/orm/mapping_api.html#sqlalchemy.orm.declarative_base>* > as Any (code > <https://github.com/sqlalchemy/sqlalchemy/blob/9a8d039716068ded890e726ba344620907d86170/lib/sqlalchemy/orm/decl_api.py#L762-L772>, > see also this SO conversation > <https://stackoverflow.com/questions/58325495/what-type-do-i-use-for-sqlalchemy-declarative-base>) > such that I have to use: > > * CustomBase: typing.Any = create_base(schema="foo")* > > Is there a better way to type-hint *CustomBase* here?
yes use the recipe given at https://docs.sqlalchemy.org/en/14/orm/declarative_styles.html#creating-an-explicit-base-non-dynamically-for-use-with-mypy-similar . That is SQLAlchemy 1.4 documentation. SQLAlchemy 2.0, when released, moves "declarative_base()" to legacy status for this reason and provides a new base class to use. > > On Thursday, July 28, 2022 at 10:09:26 PM UTC+10 jens.t...@gmail.com wrote: >> Hello, >> >> I’m using a *Base = declarative_base() >> <https://docs.sqlalchemy.org/en/14/orm/mapping_api.html#sqlalchemy.orm.declarative_base>* >> in a single, shared package that other packages can import and use. Now >> suppose I have two more packages, each of which declares a bunch of mappings >> using that one common *Base*. >> >> Is it possible to separate the mappings? I noticed that after importing both >> packages, *Base.metadata.tables.keys()* lists all table mappings from both >> packages. >> >> However, I’d like to treat the tables from these two packages separately and >> bind them to different engines (and databases). >> >> Is that possible? >> >> Much thanks! >> Jens > > > -- > 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/6a2ce911-f5ec-46f9-bb75-62a36005d2ban%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/6a2ce911-f5ec-46f9-bb75-62a36005d2ban%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/fc9ceaea-03c8-40e1-99cb-1ff2a53299d1%40www.fastmail.com.