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?