2017-06-06 4 views
1

私は、postgresql(9.5)データベースに複数のテーブルを作成しようとしてsqlalchemy(Python 2.7)を使用しています。 ubuntu 16.04sqlalchemyを使用して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

:ここでは、同じトピックに関するいくつかのより多くの記事があります。おそらくあなたは新鮮なデータベースで始まっていないでしょうか? (おそらく、 'productrecord'テーブルにプライマリキーや' productcode'の一意制約がないので、 'filerecord'テーブルがあります。 – univerio

+0

あなたは100%正しいです。ありがとう。私はすべてのテーブルを落として、再び走った。魅力のように働いた。申し訳ありません、質問のために。投稿する前にそれを試してください。 – buckc

答えて

0

univerioさんのコメント@上記の問題を解決しますが、そのソリューションは、生産環境では有効ではありません。

スタックトレースと最後にエラーメッセージとソースコード - すべてのテーブルを削除するだけではオプションではありません。データベース管理の普及した(そして非常に強力な)方法の1つ、およびそれに続く付随する移行は、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/