问题
The Model is below plus error message below that.
I am trying to create some array columns using Alembic but getting NameError: name 'String' is not defined.
Any help valued.
thanks!
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$
回答1:
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))
to
postgresql.ARRAY(sa.String(), nullable=True))
来源:https://stackoverflow.com/questions/36865799/alembic-sqlalchemy-postgres-nameerror-name-string-is-not-defined-trying-to