给定一组列名及其类型,目标是
实例化一个表和相应的映射类。sqlalchemy动态创建表和映射类
它与此处发布的问题有关:Dynamic Class Creation in SQLAlchemy。
到目前为止,我有以下几点:
table = Table(tbl,
metadata,
*(Column(col, ctype, primary_key=pk, index=idx) for col, ctype, pk, idx in zip(attrs, types, primary_keys, indexes))
)
这将创建表对象。现在我需要创建相应的类。
mydict={'__tablename__':tbl}
cls = type(cls_name, (Base,), mydict)
这给了我以下错误:
ArgumentError: Mapper Mapper|persons_with_coord|t_persons_w_coord could not assemble any primary key columns for mapped table
我的问题是如何指定的主键作为类创建的一部分。 并创建下课后,我需要调用映射如下:
mapper(cls, table)