On Fri, Jul 29, 2022, at 6:57 PM, [email protected] wrote:
> Thank you, Mike!
> 
> I’m still a little unclear: how would I use different *MetaData* for 
> different sets of tables? For example, currently I use this function:
> 
> *def create_base(schema: typing.Optional[str] = None) -> typing.Any:
>     return declarative_base(
>         cls=_Base,*
> *        metadata=MetaData(schema=schema, 
> naming_convention=_NAMING_CONVENTION),*
> *        metaclass=_BaseMeta
>     )*
> where I have a custom *_Base* (to supply e.g. useful *__str__* etc. helpers) 
> and a custom *_BaseMeta* which derives from *DeclarativeBase*. The 
> *MetaData*’s schema is a parameter to that function so that I can create 
> different *Base* classes with their own *MetaData*. Using these different 
> *Base* classes I then declare different table mappings, each with their own 
> *MetaData*.

pep-484 typing is entirely not compatible with functions that return classes 
unfortunately, so if you wanted Base to be recognized as a class, you have to 
declare it as needed.  The quickest way to do this is likely with the 
as_declarative_base function:

mapper_registry = registry()

@mapper_registry.as_declarative_base()
class Base:
    pass

Above, Base is now  a new declarative base with its own MetaData collection.





> 
> Or… would I now statically declare a class 
> *CommonBase(metaclass=DeclarativeMeta)* (contains the helpers, etc.) and then 
> derive specific classes with their respective *MetaData*, e.g.
> 
> *class Base(CommonBase):*
> *    metadata = MetaData(schema=schema, naming_convention=_NAMING_CONVENTION)*
> 
> Or… maybe I’m thinking about this the wrong way?

right we have this in SQLAlchemy 2.0 called DeclarativeBase and it works 
exactly that way.   To get that in 1.4, here's one that I had to look in the 
current source for DeclarativeMeta to figure out

class CommonMeta(DeclarativeMeta):
    def __init__(cls, classname, bases, dict_, **kw):
        if not bases or CommonBase in bases:
            cls.registry = cls._sa_registry = reg = registry()
            cls.metadata = reg.metadata
            cls.__init__ = reg.constructor
            type.__init__(cls, classname, bases, dict_)
        else:
            super().__init__(classname, bases, dict_, **kw)

class CommonBase(metaclass=CommonMeta):
    pass



class Base(CommonBase):
    pass


> 
> Jens
> 
> 
> On Saturday, July 30, 2022 at 6:47:24 AM UTC+10 Mike Bayer wrote:
>> 
>> 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.
>> 
> 
> 
> -- 
> 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/b5d7be5a-8713-491f-bef4-b662f7d7cc53n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/b5d7be5a-8713-491f-bef4-b662f7d7cc53n%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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/327106fa-e0b1-484e-8c63-4ba7ca083246%40www.fastmail.com.

Reply via email to