Python SqlAlchemy - AttributeError: mapper

前端 未结 1 1399
抹茶落季
抹茶落季 2021-01-28 13:34

based on my model:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import          


        
相关标签:
1条回答
  • 2021-01-28 13:59

    Looking at the traceback you can see these lines:

      ...
      File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1653, in do_init
        self._process_dependent_arguments()
      File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1710, in _process_dependent_arguments
        self.target = self.mapper.mapped_table
      ...
    

    which narrow your problem down quite a bit. The relationship

        user = relationship('model.user.User', back_populates='sessions')
    

    uses a Python evaluable string as the argument, the use of which is further explained in "Configuring Relationships":

    Relationships to other classes are done in the usual way, with the added feature that the class specified to relationship() may be a string name. The “class registry” associated with Base is used at mapper compilation time to resolve the name into the actual class object, which is expected to have been defined once the mapper configuration is used

    If you've not imported models.user module anywhere before you try to instantiate a Session object for the first time, then the name resolving fails because the class User has not been created yet and does not exist in the registry. In other words for the name resolving to work, all classes must have been defined, which means that their bodies must have been executed.

    And if you actually have imported the models.user module, check your other models and that their related model classes have been defined. Using your models for the first time triggers mapper compilation/configuration, so the source of the error could be other models as well.

    0 讨论(0)
提交回复
热议问题