1
我遇到了一個問題,試圖測試是否可以將一些記錄保存到我的數據庫中。我使用postgres.app作爲數據庫。 之前,我盡我的測試中,我所做的:一對多,Flask-SqlAlchemy,sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)關係不存在
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。 你能提出一些解決這個問題的方法嗎?