Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy.exc.InvalidRequestError: Multiple classes found for path in the registry of this declarative base

I keep getting below error when I flask sqlalchemy app for this model : sqlalchemy.exc.InvalidRequestError: Multiple classes found for path "Address" in the registry of this declarative base. Please use a fully module-qualified path.

I mostly referred this : http://flask-sqlalchemy.pocoo.org/2.3/quickstart/#simple-relationships

# The examples in this file come from the Flask-SQLAlchemy documentation
# For more information take a look at:
# http://flask-sqlalchemy.pocoo.org/2.1/quickstart/#simple-relationships

from datetime import datetime

from rest_api_demo.database import db


class Student(db.Model):
    __table_args__ = {'extend_existing': True} 
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(80))
    address_id = db.Column(db.Integer, db.ForeignKey('address.id'),nullable=False)
    address = db.relationship('Address',backref=db.backref('students',lazy='dynamic'))


    def __init__(self, first_name,addr=None):
        self.first_name = first_name
        self.addr = address

class Address(db.Model):
    #__tablename__ = 'address'
    __table_args__ = {'extend_existing': True} 
    id = db.Column(db.Integer,primary_key = True)
    street = db.Column(db.String(200))


 def __init__(self, street):
        self.street = street
like image 646
Ameyj Avatar asked Oct 12 '17 02:10

Ameyj


1 Answers

I've got this error sometimes when I did a refactoring of code without running any migrations of the database.

What Happens is that Your model name don't have a tablename in Your database to store any relations.

like image 120
Paal Pedersen Avatar answered Oct 16 '22 23:10

Paal Pedersen