映射程序无法组装任何主键列

2024-03-29 10:50:14 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我已经从sqllite表创建了一个tmp表,它是基于各种选择条件的原始表的子集。截图中有一个样本。

我试图一次循环遍历一个表记录,以便更新每个记录中的一个字段。我有:

source_table= self.source
engine = create_engine(db_path)
Base = declarative_base()
# metadata = Base.metadata
# Look up the existing tables from database
Base.metadata.reflect(engine)

# Create class that maps via ORM to the database table
table = type(source_table, (Base,), {'__tablename__': source_table})

Session = sessionmaker(bind=engine)
session = Session()
i = 0
for row in session.query(table).limit(500):

    i += 1
    print object_as_dict(row)

但这给了我们:

ArgumentError: Mapper Mapper|tmp|tmp could not assemble any primary key columns for mapped table 'tmp'

如何执行此循环?


Tags: thesourceforbasesession记录tabledatabase