1

I ran into a problem trying to test if I can persist some records into my database. I use postgres.app as database. Before I try my test, I did:

dropdb testdb
createdb testdb
python db_create.py

My models.py looks like this:

class Municipality(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    municipality_name = db.Column(db.String(120), unique=True)
    population = db.Column(db.Integer)
    region_id = db.Column(db.Integer, db.ForeignKey('region.id'))

    def __init__(self, municipality_name, population, region_id):
        self.municipality_name = municipality_name
        self.population = population
        self.region_id = region_id

    def __repr__(self):
        return '{0}'.format(self.municipality_name)

class Region(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    region_name = db.Column(db.String(120), unique=True)
    region_capital = db.Column(db.String(120), unique=True)
    population = db.Column(db.Integer)
    municipalities = db.relationship('Municipality', backref=db.backref('region'))

    def __init__(self, region_name, region_capital, population):
        self.region_name = region_name
        self.region_capital = region_capital
        self.population = population

    def __repr__(self):
        return '{0}'.format(self.region_name)

I try

todb = Region(region_name='Ahvenanmaa',region_capital='Maarianhamina',population=123)
db.session.add(todb)
db.session.commit()

And already at this step I get an error:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "region" does not exist
LINE 1: INSERT INTO region (region_name, region_capital, population)...
                    ^
 [SQL: 'INSERT INTO region (region_name, region_capital, population) VALUES (%(region_name)s, %(region_capital)s, %(population)s) RETURNING region.id'] [parameters: {'population': 123, 'region_capital': 'Maarianhamina', 'region_name': 'Ahvenanmaa'}]

I guess I'm missing some step I had to do after python db_create.py. Can you suggest something to approach this issue?

1 Answer 1

1

Looks like you haven't done (first) migrations. I believe you need to $ ./db_migrate.py

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.