使用SQLAlchemy作为python的ORM-不存在关系

时间:2019-08-12 10:25:58

标签: python sqlalchemy

我在python中遇到sqlalchemy问题。

我有以下文件:

base.py:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:mysecretpassword@localhost:5432/postgres',echo=True)
Base = declarative_base(engine)

Product.py:

from sqlalchemy import Table,Date,TEXT,Column,BIGINT,Integer,Boolean
from base import Base    

class Product(Base):

    __tablename__ = 'products'
    id = Column('id',BIGINT, primary_key=True)
    barcode = Column('barcode' ,BIGINT)
    productName = Column('name', TEXT)
    productType = Column('type', Integer)
    maufactureName=Column('maufacture_name',TEXT,nullable=True)
    manufactureCountry = Column('manufacture_country', TEXT)
    manufacturerItemDescription = Column('manufacture_description',TEXT)
    unitQuantity=Column('uniq_quantity',Integer)
    quantity=Column('quantity',Integer)
    quanityInPackage=Column('quantity_in_package',Integer)
    isWeighted=Column('is_weighted',Integer)
    picture=Column('picture_url',TEXT)


    def __init__(self,args...):
   .....

main.py:     从产品进口产品     从sqlalchemy导入create_engine     从sqlalchemy.orm导入sessionmaker     从基地进口基地     Base.metadata.create_all()     会话= sessionmaker()     session = Session()     session.add(产品(...))     session.commit()

运行主程序时,我不断收到错误消息,指出产品关系不存在:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "products" does not exist

知道为什么吗?从sqlalchemy日志看来,它甚至都没有尝试创建表

1 个答案:

答案 0 :(得分:0)

不确定最初的根本原因是什么,但是通过遵循注释中的帮助,我得以解决了问题。我将更新的代码保留在帖子中,以便其他面临相同问题的人也可以看到解决方案

相关问题