miguelgrinberg / flasky

Companion code to my O'Reilly book "Flask Web Development", second edition.
MIT License
8.54k stars 4.21k forks source link

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

Closed labike closed 3 years ago

labike commented 3 years ago

when i try insert admin or role, i get the error:

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

and before, i use db.metadata.clear() clear db mapping. i don't know the error in where, Can you help me see where the error? thanks miguelgrinberg!!!

models.py

from app import db
from datetime import datetime

db.metadata.clear()

class User(db.Model):
    """
    会员, info简介, face图像, add_time注册时间, uuid唯一标识
    """
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    pwd = db.Column(db.String(100))
    email = db.Column(db.String(100), unique=True)
    phone = db.Column(db.String(11), unique=True)
    info = db.Column(db.Text)
    face = db.Column(db.String(255), unique=True)
    add_time = db.Column(db.DateTime, index=True, default=datetime.now)
    uuid = db.Column(db.String(255), unique=True)
    user_logs = db.relationship('Userlogs', backref='user')  # 会员日志外键关联
    comments = db.relationship('Comment', backref='user')
    moviecols = db.relationship('Moviecol', backref='user')

    def __repr__(self):
        return '<User %r>' % self.name

class Userlogs(db.Model):
    """
    会员登录日志
    """
    __tablename__ = 'user_logs'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # db.ForeignKey()创建外键
    ip = db.Column(db.String(100))
    add_time = db.Column(db.DateTime, index=True, default=datetime.now)

    def __repr__(self):
        return '<Userlogs %r>' % self.id

if __name__ == '__main__':
    # db.create_all()
    """
    role = Role(
        name='超级管理员',
        auths=''
    )
    db.session.add(role)
    db.session.commit()
    """
    from werkzeug.security import generate_password_hash

    admin = Admin(
        name='admin',
        pwd=generate_password_hash('123456'),
        is_super=0,
        role_id=1
    )
    db.session.add(admin)
    db.session.commit()
截屏2021-07-21 上午9 20 42
miguelgrinberg commented 3 years ago

I think this must have to do with the structure of your project. Can you provide a complete example? Feel free to create a repo on GitHub with all the code.

labike commented 3 years ago

@miguelgrinberg thanks, I will upload my completed project in my git repository.

labike commented 3 years ago

@miguelgrinberg i'm network terrible and code too large, so i upload my cloud, i don't know can you download? thank you very much! 2021-07-22_101957

miguelgrinberg commented 3 years ago

Sorry, but the download does not work. Can you please simplify your application so that it can be uploaded to github? Thanks.

labike commented 3 years ago

@miguelgrinberg ok, i'm try again.

labike commented 3 years ago

@miguelgrinberg i'm delete static source, rest python code and html. repository i'm upload all source code to google drive and share you. thanks help me!!! 2021-07-23_144801

miguelgrinberg commented 3 years ago

Everything works for me. Here is a copy of my test:

>>> from app import db
>>> from app.models import Role, Admin
>>> db.create_all()
>>> role = Role(name='admin', auths='')
>>> db.session.add(role)
>>> db.session.commit()
>>> role.id
1
>>> admin = Admin(name='admin', pwd='123', is_super=0, role_id=1)
>>> db.session.add(admin)
>>> db.session.commit()
>>> Admin.query.all()
[<Admin 'admin'>]
labike commented 3 years ago

@miguelgrinberg thanks! may be db migration has error.