1

I have a model with a PasswordType column (from SQLAlchemy-Utils):

class User(Base):
    __tablename__ = 'users'
    id = sa.Column(sa.Integer(), primary_key=True)
    username = sa.Column(sa.String(length=80), nullable=False, unique=True)
    password = sa.Column(PasswordType(max_length=500), nullable=False)

after experimenting a lot, i devised the following Alembic script to create this table:

op.create_table('users',
                sa.Column('id', sa.Integer()),
                sa.Column('username', sa.String(length=80), nullable=False),
                sa.Column('password', sa.String(length=500), nullable=True),
                sa.PrimaryKeyConstraint('id'))
op.create_index('ix_users_username', 'users', ['username'], unique=True)
op.alter_column('users', 'password',
                existing_type=sa.String(length=500),
                type_=PasswordType(max_length(500),
                nullable=True,
                postgresql_using='password::bytea')

The magic postgresql_using parameter of alter_column makes sure that the password field will become of type bytea if the dialect is postgresql. However, if i pass this parameter to create_table, i get the following error:

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 3eecda601e5a, Create the user model
Traceback (most recent call last):
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/config.py", line 559, in main
    CommandLine(prog=prog).main(argv=argv)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/config.py", line 553, in main
    self.run_cmd(cfg, options)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/config.py", line 530, in run_cmd
    fn(
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/command.py", line 294, in upgrade
    script.run_env()
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/script/base.py", line 490, in run_env
    util.load_python_file(self.dir, "env.py")
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/util/pyfiles.py", line 97, in load_python_file
    module = load_module_py(module_id, path)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/util/compat.py", line 182, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "alembic/env.py", line 82, in <module>
    run_migrations_online()
  File "alembic/env.py", line 76, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/runtime/environment.py", line 813, in run_migrations
    self.get_context().run_migrations(**kw)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/runtime/migration.py", line 560, in run_migrations
    step.migration_fn(**kw)
  File "$HOME/Verkefni/python/user-manager/alembic/versions/3eecda601e5a_create_the_user_model.py", line 20, in upgrade
    op.create_table('users',
  File "<string>", line 8, in create_table
  File "<string>", line 3, in create_table
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/operations/ops.py", line 1109, in create_table
    return operations.invoke(op)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/operations/base.py", line 354, in invoke
    return fn(self, operation)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/operations/toimpl.py", line 100, in create_table
    table = operation.to_table(operations.migration_context)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/operations/ops.py", line 1026, in to_table
    return schema_obj.table(
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib/python3.9/site-packages/alembic/operations/schemaobj.py", line 143, in table
    t = sa_schema.Table(name, m, *columns, **kw)
  File "<string>", line 2, in __new__
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/util/deprecations.py", line 139, in warned
    return fn(*args, **kwargs)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/sql/schema.py", line 563, in __new__
    metadata._remove_table(name, schema)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.raise_(
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/sql/schema.py", line 558, in __new__
    table._init(name, metadata, *args, **kw)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/sql/schema.py", line 641, in _init
    self._extra_kwargs(**kwargs)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/sql/schema.py", line 773, in _extra_kwargs
    self._validate_dialect_kwargs(kwargs)
  File "$HOME/.cache/pypoetry/virtualenvs/user-manager-sPsSOABE-py3.9/lib64/python3.9/site-packages/sqlalchemy/sql/base.py", line 312, in _validate_dialect_kwargs
    raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Argument 'postgresql_using' is not accepted by dialect 'postgresql' on behalf of <class 'sqlalchemy.sql.schema.Table'>

Is there a way to make the column type right at create_table time? Also, it should only be set for the PostgreSQL dialect; if the user of my app wants to use MySQL as a backend, the default type of PasswordType works just fine.

GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69

0 Answers0