from lin import db
from lin.core import User as _User
from sqlalchemy import Column, String
from sqlalchemy.orm import relationship
from app.models.association_tables import like_and_quota
from lin import db
from lin.exception import NotFound
from lin.interface import InfoCrud as Base
from sqlalchemy import Column, Integer, Text, ForeignKey
from sqlalchemy.orm import relationship
from app.models.category import Category
from app.models.association_tables import like_and_quota
def create_app(register_all=True, environment='production'):
app = LinFlask(name, static_folder='./assets')
app.config['ENV'] = environment
env = app.config.get('ENV')
if env == 'production':
app.config.from_object('app.config.setting.ProductionConfig')
app.config.from_object('app.config.secure.ProductionSecure')
elif env == 'development':
app.config.from_object('app.config.setting.DevelopmentConfig')
app.config.from_object('app.config.secure.DevelopmentSecure')
app.config.from_object('app.config.log')
if register_all:
register_blueprints(app)
from app.models.user import User
Lin(app, user_model=User)
register_before_request(app)
register_after_request(app)
apply_cors(app)
创建所有表格
create_tables(app)
return app
+ 报错信息
sqlalchemy.exc.InvalidRequestError: Multiple classes found for path "User" in the registry of this declarative base. Please use a fully module-qualified path.
使用扩展过的 user_model 模型(类名为
User
)建立多对多关系,程序可以跑起来,但是接收 http 请求时就会报错like_and_quota
from sqlalchemy import Table, Column, Integer, ForeignKey from lin.interface import InfoCrud as Base
用户点赞的语录
like_and_quota = Table('like_and_quota', Base.metadata, Column('user_id', Integer, ForeignKey('lin_user.id')), Column('quota_id', Integer, ForeignKey('quota.id')))
/app/models/user.py
from lin import db from lin.core import User as _User from sqlalchemy import Column, String from sqlalchemy.orm import relationship from app.models.association_tables import like_and_quota
class User(_User): wx_openid = Column(String(190)) # 微信 openid liked_quotas = relationship('Quota', secondary=like_and_quota, back_populates='liked_users') # 该用户点赞的语录(与 Quota 模型建立多对多关系)
/app/models/quota.py
from lin import db from lin.exception import NotFound from lin.interface import InfoCrud as Base from sqlalchemy import Column, Integer, Text, ForeignKey from sqlalchemy.orm import relationship from app.models.category import Category from app.models.association_tables import like_and_quota
class Quota(Base): id = Column(Integer, primary_key=True, autoincrement=True) content = Column(Text) # html 格式 content_text = Column(Text) # 纯文本格式 category_id = Column(Integer, ForeignKey('category.id')) category = relationship('Category', back_populates='quotas') # 所属分类 liked_users = relationship('User', secondary=like_and_quota ,back_populates='liked_quotas') # 点赞该语录的用户
def create_app(register_all=True, environment='production'): app = LinFlask(name, static_folder='./assets') app.config['ENV'] = environment env = app.config.get('ENV') if env == 'production': app.config.from_object('app.config.setting.ProductionConfig') app.config.from_object('app.config.secure.ProductionSecure') elif env == 'development': app.config.from_object('app.config.setting.DevelopmentConfig') app.config.from_object('app.config.secure.DevelopmentSecure') app.config.from_object('app.config.log') if register_all: register_blueprints(app) from app.models.user import User Lin(app, user_model=User) register_before_request(app) register_after_request(app) apply_cors(app)
创建所有表格
sqlalchemy.exc.InvalidRequestError: Multiple classes found for path "User" in the registry of this declarative base. Please use a fully module-qualified path.
/app/models/user.py
class CUser(_User):
/app/models/quota.py
liked_users = relationship('CUser', secondary=like_and_quota ,back_populates='liked_quotas') # 点赞该语录的用户
/app/app.py
from app.models.user import CUser Lin(app, user_model=CUser)