Originally posted by **mmcmNew** May 31, 2024
I have 3 databases connected through bind. When I try to create my database without specifying binds, they are created in the main database without any problems and work fine, but I need them in the second database. When I create them there, the database appears and the tables are created, but when creating many-to-many relationships, I get an error.
> 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 '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.
Discussed in https://github.com/pallets-eco/flask-sqlalchemy/discussions/1341