如何使用sqlalchemy添加citext列?

时间:2017-09-27 05:01:22

标签: python flask sqlalchemy psycopg2 flask-appbuilder

我在postgresql表中有一个带有citext数据类型的列。我想将该列添加到flask appbuilder中的模型中。

下载sqlalchemy-citext库并添加如下。 在flask app builder中创建了以下模型。

class TestUsers1(Model):

    __bind_key__ = 'pg'
    __table_args__ = {"schema": "sample"}
    uuid = Column(UUID(as_uuid=True), primary_key=True,default=uuid.uuid4)
    company_code = Column(CIText())
    first_name = Column(String(255))

    def __repr__(self):
       return self.name

执行相关视图时出现以下错误。

Traceback (most recent call last):
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/security/decorators.py", line 26, in wraps
    return f(self, *args, **kwargs)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/views.py", line 478, in list
    widgets=widgets)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/baseviews.py", line 158, in render_template
    return render_template(template, **dict(list(kwargs.items()) + list(self.extra_args.items())))
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 2, in top-level template code
    {% import 'appbuilder/general/lib.html' as lib %}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/base.html", line 1, in top-level template code
    {% extends base_template %}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/baselayout.html", line 2, in top-level template code
    {% import 'appbuilder/baselib.html' as baselib %}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/init.html", line 46, in top-level template code
    {% block body %}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/baselayout.html", line 19, in block "body"
    {% block content %}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 7, in block "content"
    {% block list_search scoped %}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 8, in block "list_search"
    {% call lib.accordion_tag("accordion1",_("Search"), False) %}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/jinja2/runtime.py", line 553, in _invoke
    rv = self._func(*arguments)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/general/lib.html", line 254, in template
    {{ caller() }}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/jinja2/runtime.py", line 553, in _invoke
    rv = self._func(*arguments)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 9, in template
    {{ widgets.get('search')()|safe }}
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/flask_appbuilder/widgets.py", line 107, in __call__
    label_columns[col] = as_unicode(self.template_args['form'][col].label.text)
  File "/home/manunra/Envs/dev_qed/lib/python3.5/site-packages/wtforms/form.py", line 65, in __getitem__
    return self._fields[name]
KeyError: 'company_code'

有没有办法在sqlalchemy中添加citext列?

1 个答案:

答案 0 :(得分:0)

该错误似乎与CIText无关,似乎是wtforms的问题。