Alembic migration issue with PostgreSQL schema and invalid one-to-many relationship

I have created a database and schema in Postgres. I have my models and when I run python manager.py db migrate

that uses Flask-Migrate I get below error. However, the team is db init

working.

sqlalchemy.exc.ProgrammingError: (ProgrammingError) no schema has been selected to create in

      

Now when I add __tablename__

and __table_args__ = {"schema": "name_of_schema"}

to my models, I get the following error: db init

and db migrate

:

sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'deploy.instance_id' could not find table 'instance' with which to generate a foreign key to target column 'id'

      

My relationship, however, looks good. I've seen a lot of examples and they worked fine on SQLite without Flask-Migrate.

I have three tables as follows (removing most of the columns):

class Application(db.Model):
    __tablename__ = 'application'
    __table_args__ = {"schema":"v1"}
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=False)
    instances = db.relationship('Instance', backref='application', lazy='dynamic')

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


class Instance(db.Model):
    __tablename__ = 'instance'
    __table_args__ = {"schema":"v1"}
    id = db.Column(db.Integer, primary_key=True)
    host = db.Column(db.String(80), unique=False)
    application_id = db.Column(db.Integer, db.ForeignKey('application.id'))
    deploys = db.relationship('Deploy', backref='instance', lazy='dynamic')

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


class Deploy(db.Model):
    __tablename__ = 'deploy'
    __table_args__ = {"schema":"v1"}
    id = db.Column(db.Integer, primary_key=True)
    tag = db.Column(db.String(80), unique=False)
    instance_id = db.Column(db.Integer, db.ForeignKey('instance.id'))

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

      

Relations:

  • Instance application (one-to-many, one application in many cases)
  • Deployment Instance (one-to-many, one instance of many deployments)

When I remove all the relationships and create a separate table, I still get the first mistake sqlalchemy.exc.ProgrammingError: (ProgrammingError) no schema has been selected to create in

. What is the problem and how can I fix it?

+3


source to share


1 answer


Postgresql has a "default schema" concept, which is set using SEARCH_PATH. This looks like the user you are connecting as it is not configured. Take a look at Client Connection Settings on the Postgresql website.

For a foreign key error, when you use an explicit argument schema

with Table

, that name must be specified in objects as well ForeignKey

:



ForeignKey('myschema.mytable.id')

      

+6


source







All Articles