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 '<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.

Reply via email to