Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant find Postgres schema and one-to-many relationship error. #35

Closed
nayefc opened this issue Aug 28, 2014 · 4 comments
Closed

Cant find Postgres schema and one-to-many relationship error. #35

nayefc opened this issue Aug 28, 2014 · 4 comments

Comments

@nayefc
Copy link

nayefc commented Aug 28, 2014

I've created a database and schema in Postgres. I have my models and when I run python manager.py db migrate which uses Flask-Migrate, I get the error below. However, the db init command works.

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 error below for both, 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 relationships however, look okay. I've seen many examples and they worked properly on SQLite without Flask-Migrate.

I have three tables as follows (removing most 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)

The relationships are:

  • Application to Instance (one-to-many; one application many instances)
  • Instance to Deploy (one-to-many; one instance many deploys)

What am I doing here? Why are my relationships wrong? When I remove all relationships and create a standalone table, I still get the first error: sqlalchemy.exc.ProgrammingError: (ProgrammingError) no schema has been selected to create in.

Any help is much appreciated.

@nayefc
Copy link
Author

nayefc commented Aug 29, 2014

I think the issue comes down to tying Flask-Migrate (or Alembic) to a Postgres schema. How can I do that?

@miguelgrinberg
Copy link
Owner

I'm not very knowledgeable in Postgres, but have you set the search_path to include the schema in which your table exists?

@nayefc
Copy link
Author

nayefc commented Sep 2, 2014

I did that. I figured it out. Apart from that and setting __table_args__ = {"schema":"v1"}, the schema needs to be specified in ForeignKey such that: ForeignKey('myschema.table.id')

@nayefc nayefc closed this as completed Sep 2, 2014
@tomitsui123
Copy link

I did that. I figured it out. Apart from that and setting __table_args__ = {"schema":"v1"}, the schema needs to be specified in ForeignKey such that: ForeignKey('myschema.table.id')

You save my life. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants