我正在使用sqlalchemy + alembic + Flask,我無法映射圓形類。sqlalchemy + flask:class沒有定義
應用/用戶/ models.py:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
email = Column(String)
password = Column(String)
session = relationship("Session", back_populates='user', cascade='all,delete', lazy='dynamic')
notes = relationship('Note2User', back_populates='user', cascade='all,delete', lazy='dynamic')
應用/筆記/ models.py:
class Note2User(Base):
__tablename__ = 'notes_users_m2m'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), nullable=False)
user = relationship('User', back_populates='notes')
note_id = Column(Integer, ForeignKey('notes.id', ondelete='CASCADE'), nullable=False)
note = relationship('Note', back_populates='users')
表Note2User爲M2M關係用戶作出< - >注,但是當我開始應用程序並做了一些請求,得到錯誤:
InvalidRequestError: When initializing mapper Mapper|User|users, expression 'Note2User' failed to locate a name ("name 'Note2User' is not defined"). If this is a class name, consider adding this relationship() to the class after both dependent classes have been defined.
正在初始化分貝db/初始化的.py:(dunder名)
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
engine = create_engine('postgresql+psycopg2://server:[email protected]:5432/test')
Base = declarative_base()
meta = MetaData()
meta.reflect(bind=engine)
db_session = Session(bind=engine)