我遇到了一个问题,试图测试我是否可以将某些记录保存到我的数据库中。我使用postgres.app作为数据库。 在我尝试测试之前,我做了:
dropdb testdb
createdb testdb
python db_create.py
我的models.py看起来像这样:
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)
我试试
todb = Region(region_name='Ahvenanmaa',region_capital='Maarianhamina',population=123)
db.session.add(todb)
db.session.commit()
已经在这一步我得到一个错误:
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'}]
我想我错过了python db_create.py后我必须做的一些步骤。 你能建议一些方法来解决这个问题吗?
答案 0 :(得分:1)
看起来你还没有完成(第一次)迁移。我相信你需要$ ./db_migrate.py