'flask creating models with ’backref‘’ raise exception?

​ trying to create model.py with 'backref' parameter

sqlalchemy.exc.ArgumentError: Error creating backref 'user' on relationship 'User.to_blog': property of that name exists on mapper 'mapped class Blog->blog'

some code follows:

from app import db
from datetime import datetime


class User(db.Model):
    __tablename__ = "user"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), unique=True, nullable=False)
    pwd = db.Column(db.String(255), nullable=False)
    email = db.Column(db.String(255))
    introduce = db.Column(db.Text, default="xxx")
    headName = db.Column(db.String(255), default='default.png')
    head = db.Column(db.LargeBinary(1048576))
    right = db.Column(db.Integer, default=2)

    # backref
    to_blog = db.relationship('Blog', backref='user')    # error



    def __repr__(self):
        return "User:%s %s %s" % (self.name, self.pwd, self.id)

    


class Blog(db.Model):
    __tablename__ = "blog"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    content = db.Column(db.Text)
    body_html = db.Column(db.Text)
    title = db.Column(db.String(255), nullable=False, unique=True)
    headName = db.Column(db.String(255), default='default_blog.png')
    head = db.Column(db.LargeBinary(1048576))

    # ForeignKey
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    
    
    
if __name__ == "__main__":
    db.drop_all()
    db.create_all()
    # #
    roles = User(id=2, name='cat', pwd='cat', email='[email protected]')
    db.session.add(roles)
    db.session.commit()
 

and raise exception

sqlalchemy.exc.InvalidRequestError: Multiple classes found for path "Blog" in the registry of this declarative base. Please use a fully module-qualified path.

so i change it to

    to_blog = db.relationship('app.models.Blog', backref='user') 

but why raise the exception



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source