gpt4 book ai didi

python - Flask - (ProgrammingError) - 构建postgresql数据库时的外键约束

转载 作者:太空狗 更新时间:2023-10-30 03:00:52 26 4
gpt4 key购买 nike

我目前正在尝试将我的 Flask 应用程序部署到 Heroku,但我在数据库初始化期间遇到错误。

这是我的 models.py 文件:

from app import app  
from app import db
from werkzeug.security import generate_password_hash, check_password_hash
from sqlalchemy import create_engine, Column, Integer, String, Date, ForeignKey, event, Boolean, Table
from sqlalchemy.orm import scoped_session, sessionmaker, backref, relationship

class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
password = db.Column(db.String(255), nullable=False, server_default='')
email = db.Column(db.String(255), nullable=False, unique=True, index=True)
confirmed_at = db.Column(db.DateTime())
authenticated = db.Column(db.Boolean(), nullable=False, server_default='0')
company_name = db.Column(db.String(100), nullable=False, server_default='')

roles = db.relationship('Role')


def __init__(self, email, company_name, password):
self.email = email
self.company_name = company_name
self.set_password(password)

def get_id(self):
return self.email

def is_active(self):
#True, as all users are active.
return True

def is_authenticated(self):
#"""Return True if the user is authenticated."""
return self.authenticated

def is_anonymous(self):
#False, as anonymous users aren't supported."""
return False

def set_password(self, password):
self.password = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.password, password)

def __repr__(self):
return '<User %r>' % (self.company_name)

class Role(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
user_id = db.Column(db.String, db.ForeignKey('user.id'))`

这是我的 db_create.py 文件:

#!flask/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path
db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))

初始化我的数据库时,我收到以下堆栈跟踪:

Traceback (most recent call last):
File "db_create.py", line 7, in <module>
db.create_all()
File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 895, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 887, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3404, in create_all
tables=tables)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1616, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1245, in _run_visitor
**kwargs).traverse_single(element)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 120, in traverse_single
return meth(obj, **kw)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 713, in visit_metadata
self.traverse_single(table, create_ok=True)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 120, in traverse_single
return meth(obj, **kw)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 732, in visit_table
self.connection.execute(CreateTable(table))
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 729, in execute
return meth(self, multiparams, params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 69, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 783, in _execute_ddl
compiled
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 958, in _execute_context
context)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1159, in _handle_dbapi_exception
exc_info
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 951, in _execute_context
context)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 436, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (ProgrammingError) column "id" referenced in foreign key constraint does not exist
'\nCREATE TABLE role (\n\tid SERIAL NOT NULL, \n\tname VARCHAR(100), \n\tuser_id VARCHAR, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(user_id) REFERENCES "user" (id)\n)\n\n' {}

有人知道是什么原因造成的吗?

谢谢!

最佳答案

类型不匹配问题:你为Role表定义了外键为String类型:

user_id = db.Column(db.String, db.ForeignKey('user.id'))

当它在 User 表中为 Integer 时:

id = db.Column(db.Integer, primary_key=True)

更改其中之一,以便两个字段(字段名称和外部字段名称)使用兼容的字段类型。

关于python - Flask - (ProgrammingError) - 构建postgresql数据库时的外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27692658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com