SQLAlchemy:从列表动态加载表

发布于 2024-12-07 05:10:05 字数 1402 浏览 1 评论 0原文

我正在尝试创建一个从数据库加载 100 多个表的程序,以便我可以更改用户的用户 ID 的所有外观。

我决定使用循环来使用对象数组来映射每个表,而不是单独映射所有表。这样,表定义可以存储在配置文件中并在以后更新。

到目前为止,这是我的代码:

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    meta.Session.configure(bind=engine)
    meta.engine = engine

class Table:
    tableID = ''
    primaryKey = ''
    pkType = sa.types.String()
    class mappedClass(object):
        pass


WIW_TBL = Table()
LOCATIONS_TBL = Table()

WIW_TBL.tableID = "wiw_tbl" 
WIW_TBL.primaryKey = "PORTAL_USERID"
WIW_TBL.pkType = sa.types.String()

LOCATIONS_TBL.tableID = "locations_tbl"
LOCATIONS_TBL.primaryKey = "LOCATION_CODE"
LOCATIONS_TBL.pkType = sa.types.Integer()

tableList = ([WIW_TBL, LOCATIONS_TBL])

for i in tableList:

    i.tableID = sa.Table(i.tableID.upper(), meta.metadata,
    sa.Column(i.primaryKey, i.pkType, primary_key=True),
    autoload=True,
    autoload_with=engine) 

    orm.mapper(i.mappedClass, i.tableID)

此代码返回的错误是:

sqlalchemy.exc.ArgumentError: Class '<class 'changeofname.model.mappedClass'>' already has a primary mapper defined. Use non_primary=True to create a non primary Mapper.  clear_mappers() will remove *all* current mappers from all classes.

我无法使用clear_mappers,因为它会擦除所有类,并且entity_name方案似乎不适用于此处。

似乎每个对象都想使用同一个类,尽管它们都应该有自己的实例。

有人有什么想法吗?

I am trying to create a program that loads in over 100 tables from a database so that I can change all appearances of a user's user id.

Rather than map all of the tables individually, I decided to use a loop to map each of the tables using an array of objects. This way, the table definitions can be stored in a config file and later updated.

Here is my code so far:

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    meta.Session.configure(bind=engine)
    meta.engine = engine

class Table:
    tableID = ''
    primaryKey = ''
    pkType = sa.types.String()
    class mappedClass(object):
        pass


WIW_TBL = Table()
LOCATIONS_TBL = Table()

WIW_TBL.tableID = "wiw_tbl" 
WIW_TBL.primaryKey = "PORTAL_USERID"
WIW_TBL.pkType = sa.types.String()

LOCATIONS_TBL.tableID = "locations_tbl"
LOCATIONS_TBL.primaryKey = "LOCATION_CODE"
LOCATIONS_TBL.pkType = sa.types.Integer()

tableList = ([WIW_TBL, LOCATIONS_TBL])

for i in tableList:

    i.tableID = sa.Table(i.tableID.upper(), meta.metadata,
    sa.Column(i.primaryKey, i.pkType, primary_key=True),
    autoload=True,
    autoload_with=engine) 

    orm.mapper(i.mappedClass, i.tableID)

The error that this code returns is:

sqlalchemy.exc.ArgumentError: Class '<class 'changeofname.model.mappedClass'>' already has a primary mapper defined. Use non_primary=True to create a non primary Mapper.  clear_mappers() will remove *all* current mappers from all classes.

I cant use clear_mappers as it wipes all of the classes and the entity_name scheme doesn't seem to apply here.

It seems that every object wants to use the same class, although they all should have their own instance of it.

Does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海的爱人是光 2024-12-14 05:10:05

好吧,在您的情况下,它*与您尝试映射到不同Table的相同Class。为了解决这个问题,为每个Table动态创建一个类:

class Table(object):
    tableID = ''
    primaryKey = ''
    pkType = sa.types.String()
    def __init__(self):
        self.mappedClass = type('TempClass', (object,), {})

但我更喜欢稍微干净的版本:

class Table2(object):
    def __init__(self, table_id, pk_name, pk_type):
        self.tableID = table_id
        self.primaryKey = pk_name
        self.pkType = pk_type
        self.mappedClass = type('Class_' + self.tableID, (object,), {})
# ...
WIW_TBL = Table2("wiw_tbl", "PORTAL_USERID", sa.types.String())
LOCATIONS_TBL = Table2("locations_tbl", "LOCATION_CODE", sa.types.Integer())

Well, in your case it *is the same Class you try to map to different Tables. To solve this, create a class dynamically for each Table:

class Table(object):
    tableID = ''
    primaryKey = ''
    pkType = sa.types.String()
    def __init__(self):
        self.mappedClass = type('TempClass', (object,), {})

But I would prefer slightly cleaner version:

class Table2(object):
    def __init__(self, table_id, pk_name, pk_type):
        self.tableID = table_id
        self.primaryKey = pk_name
        self.pkType = pk_type
        self.mappedClass = type('Class_' + self.tableID, (object,), {})
# ...
WIW_TBL = Table2("wiw_tbl", "PORTAL_USERID", sa.types.String())
LOCATIONS_TBL = Table2("locations_tbl", "LOCATION_CODE", sa.types.Integer())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文