-
| First Check
 Commit to Help
 Example Codefrom sqlalchemy.ext.declarative import declarative_base
from sqlmodel import SQLModel
engine_ = sqlalchemy.create_engine(MYSQL_URL)
Base = declarative_base()
SQLModel.metadata = Base.metadata
#declare all
class OldModel(Base):
    __tablname__ = "oldmodel"
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
class NewModel(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    new_value: str = Field(default=None, sa_column=Column(Text))
#create all
SQLModel.metadata.create_all(engine)DescriptionI am using  Everything worked fine so far. class OldModel(Base):
    __tablname__ = "oldmodel"
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
class NewModel(SQLModel, table=True):
    __tablename__ = 'newmodel'
    id: Optional[int] = Field(default=None, primary_key=True)
    new_value: str = Field(default=None, sa_column=Column(Text))
    oldmodel_id: Optional[int] = Field(default=None, foreign_key="oldmodel.id")
    oldmodel: Optional["OldModel"] = Relationship(sa_relationship=relationship("OldModel", lazy='joined'))Then I came to this error : ../venv/lib/python3.9/site-packages/sqlalchemy/orm/state.py:474: in _initialize_instance
    manager.dispatch.init(self, args, kwargs)
../venv/lib/python3.9/site-packages/sqlalchemy/event/attr.py:343: in __call__
    fn(*args, **kw)
../venv/lib/python3.9/site-packages/sqlalchemy/orm/mapper.py:3569: in _event_on_init
    instrumenting_mapper._check_configure()
../venv/lib/python3.9/site-packages/sqlalchemy/orm/mapper.py:1874: in _check_configure
    _configure_registries({self.registry}, cascade=True)
../venv/lib/python3.9/site-packages/sqlalchemy/orm/mapper.py:3384: in _configure_registries
    _do_configure_registries(registries, cascade)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
registries = set(), cascade = True
    @util.preload_module("sqlalchemy.orm.decl_api")
    def _do_configure_registries(registries, cascade):
    
        registry = util.preloaded.orm_decl_api.registry
    
        orig = set(registries)
    
        for reg in registry._recurse_with_dependencies(registries):
            has_skip = False
    
            for mapper in reg._mappers_to_configure():
                run_configure = None
                for fn in mapper.dispatch.before_mapper_configured:
                    run_configure = fn(mapper, mapper.class_)
                    if run_configure is EXT_SKIP:
                        has_skip = True
                        break
                if run_configure is EXT_SKIP:
                    continue
    
                if getattr(mapper, "_configure_failed", False):
                    e = sa_exc.InvalidRequestError(
                        "One or more mappers failed to initialize - "
                        "can't proceed with initialization of other "
                        "mappers. Triggering mapper: '%s'. "
                        "Original exception was: %s"
                        % (mapper, mapper._configure_failed)
                    )
                    e._configure_failed = mapper._configure_failed
>                   raise e
E                   sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class NewModel->newmodel'. Original exception was: When initializing mapper mapped class NewModel->newmodel, expression 'OldModel' failed to locate a name ('OldModel'). If this is a class name, consider adding this relationship() to the <class 'models.mymodel.NewModel'> class after both dependent classes have been defined.It seems that Base and SQLModel are not sharing the same class registry. I thought that binding the metadata to the same object would be enough but not. How can I "force" sharing class registry (if this is the issue...)? Operating SystemLinux Operating System DetailsUbuntu 21.10 SQLModel Version0.0.6 Python Version3.9 Additional ContextNo response | 
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
| It isn't due to the  | 
Beta Was this translation helpful? Give feedback.
-
| According to the docs: Is equivalent to: So you could make them share a registry by doing this: But that seems low-level fiddling. | 
Beta Was this translation helpful? Give feedback.
-
| I had a similar problem, I was trying to use DeclarativeMeta and mapper_registry at the same time. The code above allowed the problem, but add some changes to be able to generate tablename as well.  | 
Beta Was this translation helpful? Give feedback.
According to the docs:
Is equivalent to:
So you could make them share a registry by doing this:
But that seems low-level fiddling.