2

使用 sqlalchemy 创建表时我在这里遗漏了什么吗?

from sqlalchemy import create_engine
from sqlalchemy import MetaData, Column, Table, ForeignKey
from sqlalchemy import Integer, String, DateTime

engine = create_engine('postgresql+psycopg2://postgres:password#@localhost:5432/LabData',echo=True)

metadata=MetaData(bind=engine)
main_table=('sample',metadata,
            Column('LIN',String(10),primary_key=True),
            Column('material_type',String(20),nullable=False),
            Column('source',String(20),nullable=False),
            Column('material_description',String(100)),
            Column('quantity',Integer),
            Column('location',String(2)),
            Column('received_by',String(20)),
            Column('received_date',DateTime,nullable=False),
            )

metadata.create_all()

python控制台上的输出是:

2012-04-25 13:13:42,734 INFO sqlalchemy.engine.base.Engine select version()
2012-04-25 13:13:42,796 INFO sqlalchemy.engine.base.Engine {}
2012-04-25 13:13:42,796 INFO sqlalchemy.engine.base.Engine select current_schema()
2012-04-25 13:13:42,796 INFO sqlalchemy.engine.base.Engine {}

当我尝试将数据添加到表中时,它显示 nosuchtableerror。

4

1 回答 1

3

可能的错字?

main_table=('sample',metadata,...

也许应该是

main_table=Table('sample',metadata,...
#          ^^^^^
于 2012-04-25T17:33:19.970 回答