One or more mappers failed to initialize. Could not locate any relevant foreign key columns for primary join condition

好久不见. 提交于 2021-01-29 05:26:47

问题


I have tried to implement this code

 from sqlalchemy import Column, ForeignKey, Integer, String, ForeignKeyConstraint
from sqlalchemy_continuum import make_versioned , version_class, parent_class
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy as sa

params = urllib.parse.quote_plus \
(r'Driver={ODBC Driver 17 for SQL Server};Server=HP;Database=Navigation;Uid=sa;Pwd=<added>;Encrypt=yes;TrustServerCertificate=yes;Connection Timeout=30;')
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)

engine = create_engine(conn_str, echo=True)
Base = declarative_base()

make_versioned(user_cls=None)

class Article(Base):
    __versioned__ = {}
    __tablename__ = 'article'
    
    id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
    name = sa.Column(sa.Unicode(255))
    content = sa.Column(sa.UnicodeText)
    
    
sa.orm.configure_mappers()

    
version_class(Article) # ArticleHistory class
parent_class(version_class(Article)) # Article class

One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|ArticleVersion|article_version'. Original exception was: Could not locate any relevant foreign key columns for primary join condition '' on relationship ArticleVersion.version_parent. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation.

getting this error as follow -

---------------------------------------------------------------------------
InvalidRequestError                       Traceback (most recent call last)
<ipython-input-75-1a063097d434> in <module>()
     21 
     22 
---> 23 sa.orm.configure_mappers()
     24 
     25 

~\Anaconda3\lib\site-packages\sqlalchemy\orm\mapper.py in configure_mappers()
   2867                         % (mapper, mapper._configure_failed))
   2868                     e._configure_failed = mapper._configure_failed
-> 2869                     raise e
   2870                 if not mapper.configured:
   2871                     try:

InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|ArticleVersion|article_version'. Original exception was: Could not locate any relevant foreign key columns for primary join condition '' on relationship ArticleVersion.version_parent.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation.

来源:https://stackoverflow.com/questions/62679860/one-or-more-mappers-failed-to-initialize-could-not-locate-any-relevant-foreign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!