2017-06-06 74 views
1

我正在使用sqlalchemy(python 2.7)試圖在postgresql(9.5)數據庫中創建多個表。 ubuntu 16.04使用sqlalchemy在postgresql中創建表時出錯

使用sqlalchemy的抽象我出於某種原因在使用主鍵和外鍵創建表時遇到問題。

任何幫助將不勝感激。

堆棧跟蹤和錯誤信息和源代碼最後:

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源

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:[email protected]:5432/project" 
    engine = create_engine(db_string) 
    Base.metadata.create_all(engine) 
+0

它工作正常的我。也許你不是從一個新的數據庫開始的? (你可能在db中有一個現有的'filerecord'表,沒有主鍵或'productcode'上的唯一約束。) – univerio

+0

你百分之百正確。謝謝。我扔掉了所有的桌子,然後再次跑過去。像魅力一樣工作。對不起,這個問題。應該在發佈之前嘗試過。 – buckc

回答

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/