繁体   English   中英

SQLAlchemy创建动态表和列

[英]SQLAlchemy create dynamic tables and columns

我正在尝试根据我检索的数据动态创建数据库表和列。 我检索数据库列表,列名和列属性列表,如列类型,primary_key / unique,nullable以及其他元数据。 我正在尝试使用此信息动态创建表,并一直使用论坛帖子来更好地了解如何实现这一点。 所以我想根据我检索的信息创建表 - 数据库和列信息(列名和列类型,主键和可空信息。检索到的信息可以每天或每周更改。论坛帖子#1 - Sqlalchemy动态创建表格和映射类

postgresql_db = engine(...)

post_meta = sql.MetaData(bind=postgresql_db.engine)

post_meta.reflect(schema='customers')

connection = postgresql_db.engine.connect()

col_names = ['id', 'fname', 'lname', 'age']
ctype = ['Integer', 'String', 'String', 'Integer']
pk = ['True', 'False', 'False', 'False']
nulls = ['No', 'No', 'No', 'No']

class test(object):

     test = Table('customers', post_meta,
              *(Column(col, ctype, primary_key=pk, nullable=nulls)
           for col, ctype, pk, nulls in zip(col_names, ctype, pk, nulls))

test.create()

有一条错误消息: AttributeError: 'list' object has no attribute _set_parent_with_dispatch似乎无法确定此错误的确切含义。

追溯:

Traceback (most recent call last):
  File "C:/Users/xxx/db.py", line 247, in <module>
    main()
  File "C:/Users/xxx/db.py", line 168, in main
    for col, ctype, pk, nulls in zip(col_names, ctype, pk, nulls)
  File "C:/Users/xxx/apidb.py", line 168, in <genexpr>
    for col, ctype, pk, nulls in zip(col_names, ctype, pk, nulls)
  File "C:\Python27\lib\site-packages\sqlalchemy\sql\schema.py", line 1234, in __init__
    self._init_items(*args)
  File "C:\Python27\lib\site-packages\sqlalchemy\sql\schema.py", line 79, in _init_items
    item._set_parent_with_dispatch(self)
AttributeError: 'list' object has no attribute '_set_parent_with_dispatch'

我有什么想法我做错了吗?

这里有很多不正确的事情。

Column initializer中的nullable参数应该具有bool类型,但是您尝试传递str对象nulls ,对于pkprimary_key参数也是如此。

此外,您试图在理解中覆盖名称ctypepknulls ,这是不正确的并且在给定异常时会引发。 您应该在理解中重命名从zip生成的对象。

SQLAlchemy不会识别字符串'Integer''String' ,它们不是有效的Column类型。

如果要反映名为'customers'特定表,则应该only使用参数,而不是schema来完成,它应该是名称list

你也不需要课堂test

你的代码看起来像

from sqlalchemy import MetaData, Table, Column, Integer, String

postgresql_db = engine(...)

post_meta = MetaData(bind=postgresql_db.engine)

post_meta.reflect(only=['customers'])

connection = postgresql_db.engine.connect()

columns_names = ['id', 'fname', 'lname', 'age']
columns_types = [Integer, String, String, Integer]
primary_key_flags = [True, False, False, False]
nullable_flags = [False, False, False, False]

test = Table('customers', post_meta,
             *(Column(column_name, column_type,
                      primary_key=primary_key_flag,
                      nullable=nullable_flag)
               for column_name,
                   column_type,
                   primary_key_flag,
                   nullable_flag in zip(columns_names,
                                        columns_types,
                                        primary_key_flags,
                                        nullable_flags)))

test.create()

最后,如果你做post_meta.reflect(only=['customers'])并且它工作,给定的表可以简单地通过

test = post_meta.tables['customers']

没有从头开始构建。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM