Alembic SqlAlchemy Postgres“ NameError:未定义名称'String'”尝试添加Array(String)字段 [英] Alembic SqlAlchemy Postgres "NameError: name 'String' is not defined" trying to add Array(String) fields

查看:133
本文介绍了Alembic SqlAlchemy Postgres“ NameError:未定义名称'String'”尝试添加Array(String)字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型在下面,下面是错误消息。

The Model is below plus error message below that.

我正在尝试使用Alembic创建一些数组列,但得到NameError:未定义名称'String'。

I am trying to create some array columns using Alembic but getting NameError: name 'String' is not defined.

任何有价值的帮助。

谢谢!

from sqlalchemy import Column, String, Integer, DateTime
from serve_spec.db_global import db
import datetime
from time import time
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.dialects.postgresql import ARRAY

class Issues(db.Base):

    __tablename__ = 'issues'

    id = Column(String, primary_key=True)
    thread_id                   = Column(String, nullable=False)
    created                     = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.utcnow)
    created_timestamp           = Column(Integer, nullable=False, default=time)
    created_by_user_name        = Column(String, nullable=False)
    is_parent                   = Column(Integer, nullable=False)
    parent_title                = Column(String)
    subscribed                  = Column(ARRAY(String))
    unsubscribed                = Column(ARRAY(String))
    pending_notifications_web   = Column(ARRAY(String))
    pending_notifications_email = Column(ARRAY(String))
    markdown_text               = Column(String, nullable=False, )
    kernel_id                   = Column(String, nullable=False)
    state                       = Column(String, nullable=False, default='open')
    labels                      = Column(JSON())

以下是输出,底部显示错误:

Here is the output, with the error at the bottom:

(venv3.4.2) ubuntu@ip-172-31-8-128:/var/www/www.example.org/src/crowdwave$ PYTHONPATH=. alembic upgrade head
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade d9bc97e175aa -> dd9e391f807f, Issues is behind
Traceback (most recent call last):
  File "/var/www/www.example.org/venv3.4.2/bin/alembic", line 9, in <module>
    load_entry_point('alembic==0.8.5', 'console_scripts', 'alembic')()
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/config.py", line 479, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/config.py", line 473, in main
    self.run_cmd(cfg, options)
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/config.py", line 456, in run_cmd
    **dict((k, getattr(options, k)) for k in kwarg)
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/command.py", line 174, in upgrade
    script.run_env()
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/script/base.py", line 397, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/util/pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/util/compat.py", line 68, in load_module_py
    module_id, path).load_module(module_id)
  File "<frozen importlib._bootstrap>", line 539, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1614, in load_module
  File "<frozen importlib._bootstrap>", line 596, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 1220, in load
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "alembic/env.py", line 82, in <module>
    run_migrations_online()
  File "alembic/env.py", line 77, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/runtime/environment.py", line 797, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/var/www/www.example.org/venv3.4.2/lib/python3.4/site-packages/alembic/runtime/migration.py", line 312, in run_migrations
    step.migration_fn(**kw)
  File "/var/www/www.example.org/src/crowdwave/alembic/versions/dd9e391f807f_issues_is_behind.py", line 21, in upgrade
    op.add_column('issues', sa.Column('pending_notifications_email', postgresql.ARRAY(String()), nullable=True))
NameError: name 'String' is not defined
(venv3.4.2) ubuntu@ip-172-31-8-128:/var/www/www.example.org/src/crowdwave$


推荐答案

显然这是Alembic bu g:请参见 https://bitbucket.org/zzzeek/ alembic / issues / 368 / autogenerate-does-not-properly-transform

Apparently this is an Alembic bug: see https://bitbucket.org/zzzeek/alembic/issues/368/autogenerate-does-not-properly-transform

解决方法是编辑更改的迁移文件

The fix is to edit the migration file changing

 postgresql.ARRAY(String(), nullable=True))

 postgresql.ARRAY(sa.String(), nullable=True))

这篇关于Alembic SqlAlchemy Postgres“ NameError:未定义名称'String'”尝试添加Array(String)字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆