使用sqlalchemy在postgresql中创建表时出错

时间:2017-06-06 18:51:53

标签: sqlalchemy

我正在使用sqlalchemy(python 2.7)尝试在postgresql(9.5)数据库中创建多个表。 ubuntu 16.04

使用sqlalchemy的抽象我出于某种原因使用主键和外键创建表时遇到问题。

非常感谢任何帮助。

最后是

Stacktrace和错误消息和源代码:

Traceback (most recent call last):
  File "create_db.py", line 49, in <module>
    Base.metadata.create_all(engine)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3918, in create_all
    tables=tables)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1929, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1538, in _run_visitor
    **kwargs).traverse_single(element)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 121, in traverse_single
    return meth(obj, **kw)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 733, in visit_metadata
    _is_metadata_operation=True)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 121, in traverse_single
    return meth(obj, **kw)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 767, in visit_table
    include_foreign_key_constraints=include_foreign_key_constraints
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1002, in _execute_ddl
    compiled
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
    exc_info
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/home/chris/workspace/final_project/env/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
    cursor.execute(statement, parameters)

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) there is no unique constraint matching given keys for referenced table "filerecord"
 [SQL: '\nCREATE TABLE prodrecord (\n\tproductcode INTEGER NOT NULL, \n\tproductname VARCHAR(80), \n\tproductversion VARCHAR(80), \n\topsystemcode VARCHAR(50) NOT NULL, \n\tmfgcode VARCHAR(50) NOT NULL, \n\tlanguage VARCHAR(80), \n\tapplicationtype VARCHAR(80), \n\tPRIMARY KEY (productcode, opsystemcode, mfgcode), \n\tUNIQUE (productcode, opsystemcode, mfgcode), \n\tFOREIGN KEY(productcode) REFERENCES filerecord (productcode), \n\tFOREIGN KEY(opsystemcode) REFERENCES osrecord (opsystemcode), \n\tFOREIGN KEY(mfgcode) REFERENCES mfgrecord (mfgcode)\n)\n\n']

create_db.py source

from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from sqlalchemy.schema import UniqueConstraint


Base = declarative_base()

class FileRecord(Base):
    __tablename__ = 'filerecord'
    sha1 = Column(String(80))
    md5 = Column(String(80))
    crc32 = Column(String(80))
    filename = Column(String(80))
    filesize = Column(Integer)
    productcode = Column(Integer, primary_key=True, unique=True)
    opsystemcode = Column(String(50))
    specialcode = Column(String(50))

class MfgRecord(Base):
    __tablename__ = 'mfgrecord'
    mfgcode = Column(String(80), primary_key=True, unique=True)
    mfgname = Column(String(80))

class OSRecord(Base):
    __tablename__ = 'osrecord'
    opsystemcode = Column(String(80), primary_key=True, unique=True)
    opsystemname = Column(String(80))
    opsystemversion = Column(String(80))
    mfgcode = Column(String(80))

class ProdRecord(Base):
    __tablename__ = 'prodrecord'
    productcode = Column(Integer, ForeignKey('filerecord.productcode'), primary_key=True)
    productname = Column(String(80))
    productversion = Column(String(80))
    opsystemcode = Column(String(50), ForeignKey('osrecord.opsystemcode'), primary_key=True)
    mfgcode = Column(String(50), ForeignKey('mfgrecord.mfgcode'), primary_key=True)
    language = Column(String(80))
    applicationtype = Column(String(80))
    __table_args__ = (UniqueConstraint
                        (productcode, opsystemcode, mfgcode),)


if __name__ == "__main__":
    db_string = "postgres://postgres:Drpepper1@localhost:5432/project"
    engine = create_engine(db_string)
    Base.metadata.create_all(engine)

1 个答案:

答案 0 :(得分:0)

显然,@ univerio上面的评论解决了这个问题,但该解决方案在生产环境中无效 - 丢弃所有表格根本不是一个选项。管理数据库的流行(并且功能强大)方法之一,以及随后的迁移,是使用Alembic。它是由编写SQLAlchemy的同一个人编写的,我不能推荐它。以下是关于同一主题的更多文章:

https://www.compose.com/articles/schema-migrations-with-alembic-python-and-postgresql/ https://realpython.com/blog/python/flask-by-example-part-2-postgres-sqlalchemy-and-alembic/