After the tables started being created upon server startup, there were no problems, but now I get an error with every request.
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to
initialize – can’t proceed with initialization of other mappers.
Triggering mapper: ‘Mapper[Project(projects)]’. Original exception
was: When initializing mapper Mapper[Project(projects)], expression
‘task_project_relations’ failed to locate a name (“name
‘task_project_relations’ is not defined”). If this is a class name,
consider adding this relationship() to the <class
‘app.tasks.models.Project’> class after both dependent classes have
been defined.
class Task(db.Model):
__bind_key__ = 'to_do_base'
__tablename__ = 'tasks'
id = db.Column('TaskID', db.Integer, primary_key=True)
title = db.Column('Title', db.String(255))
class Project(db.Model):
__bind_key__ = 'to_do_base'
__tablename__ = 'projects'
id = db.Column('ProjectID', db.Integer, primary_key=True)
task_project_relations = db.Table('task_project_relations',
db.Column('TaskID', db.Integer, db.ForeignKey('tasks.TaskID'), primary_key=True),
db.Column('ProjectID', db.Integer, db.ForeignKey('projects.ProjectID'), primary_key=True),
bind_key='to_do_base')
Project.tasks = db.relationship('Task', secondary='task_project_relations', back_populates='projects')
Task.projects = db.relationship('Project', secondary='task_project_relations', back_populates='tasks')
name = db.Column('ProjectName', db.String(255))
My assumption is that the relationship tables are being searched for in the wrong database, but how can this happen if there is no bind_key parameter in the relations and the location of the tables is determined automatically. I don’t understand how a table can be not found if it is created when the application is created.
Mikhail is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1