hi -
I'm not sure what the issue is but if you are tinkering with metaclasses, we
first off have an alternate version of DeclarativeBase called
DeclarativeBaseNoMeta that has no metaclass installed, and may be a better
place to build off custom metaclass solutions as you wont need to be
subclassing the internal metaclass used by DeclarativeBase.
I would advise reading the source code for the DeclarativeBase /
DeclarativeBaseNoMeta classes, paying special attention to the
__init_subclass__() method which is where Declarative now does its work, and
ensuring any __init_subclass__ method on your own class is working as expected
and not interfering with the one called by SQLAlchemy's class (or just vendor
the method yourself if necessary).
On Thu, Nov 9, 2023, at 3:04 AM, 'Iwan Vosloo' via sqlalchemy wrote:
> Hi there,
>
> We are migrating our code from SqlAlchemy 1.4 to 2.0 (2.0.23 to be
> specific).
>
> We have had the following, which allowed some classes inheriting from
> our Base to use an ABCMeta metaclass:
>
> ---
> class DeclarativeABCMeta(DeclarativeMeta, ABCMeta):
> pass
>
> metadata = MetaData(naming_convention=naming_convention)
> Base = declarative_base(metadata=metadata, metaclass=DeclarativeABCMeta)
> ---
>
> The code above works fine on 2.0, but if we want heed the 2.0 docs that
> comment that declarative_base is superceded by using a class inheriting
> from DeclarativeBase, we ought to have something like:
>
> ---
> class DeclarativeABCMeta(DeclarativeMeta, ABCMeta):
> pass
>
> metadata = MetaData(naming_convention=naming_convention)
>
> class Base(DeclarativeBase, metaclass=DeclarativeABCMeta):
> """A Base for using with declarative."""
> __abstract__ = True
> metadata = metadata
> ---
>
> This, however breaks when it hits the first class inheriting from Base:
>
> ---
> class SchemaVersion(Base):
> __tablename__ = 'reahl_schema_version'
> id = Column(Integer, primary_key=True)
> version = Column(String(50))
> egg_name = Column(String(80))
> ---
>
> With:
>
> [site-packages]/sqlalchemy/orm/decl_api.py:195: in __init__
> _as_declarative(reg, cls, dict_)
> [site-packages]/sqlalchemy/orm/decl_base.py:247: in _as_declarative
> return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
> [site-packages]/sqlalchemy/orm/decl_base.py:328: in setup_mapping
> return _ClassScanMapperConfig(
> [site-packages]/sqlalchemy/orm/decl_base.py:520: in __init__
> super().__init__(registry, cls_, mapper_kw)
> [site-packages]/sqlalchemy/orm/decl_base.py:344: in __init__
> instrumentation.register_class(
> [site-packages]/sqlalchemy/orm/instrumentation.py:684: in register_class
> manager._update_state(
> [site-packages]/sqlalchemy/orm/instrumentation.py:209: in _update_state
> registry._add_manager(self)
> [site-packages]/sqlalchemy/orm/decl_api.py:1380: in _add_manager
> raise exc.ArgumentError(
> E sqlalchemy.exc.ArgumentError: Class ' 'reahl.sqlalchemysupport.sqlalchemysupport.SchemaVersion'>' already has
> a primary mapper defined.
>
> Any ideas on what we are doing wrong here?
>
> Thanks
> Iwan
>
> --
>
> --
> 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/5d616bf0-ffa7-4061-adaf-cf1c7577e0fc%40reahl.org.
--
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/8ae83685-03c3-4b80-b2a3-c4d228d94626%40app.fastmail.com.