Skip to content

prefix = autogen_context.opts["user_module_prefix"] raise KeyError #1235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
allmonday opened this issue May 5, 2023 · 2 comments
Closed
Labels
autogenerate - rendering bug Something isn't working
Milestone

Comments

@allmonday
Copy link
Contributor

Describe the bug

I've made a small snippet to generate create_table ops for my models

def create_orm_migration(metadata):

    operations = []
    for table in metadata.tables.values():
        if table.name.startswith('meeting_note'):
            operations.append(
                ops.CreateTableOp.from_table(table)
            )
            for idx in table.indexes:
                operations.append(ops.CreateIndexOp.from_index(idx))

    print(autogenerate.render_python_code(
        ops.UpgradeOps(ops=operations),
        ), 
    )

and it raises KeyError because UUID is defined in other modules.

from portal.utils.db import UUID

Base = declarative_base()

class MeetingNotes(Base, CommonFieldsMixin):
    __tablename__ = 'meeting_note_notes'

    id = Column(UUID, primary_key=True, default=uuid.uuid1)
    date = Column(Date)
    duration = Column(Integer)
    category = Column(String(1024))
    type = Column(String())
    host_employee_number = Column(Integer)
    content = Column(Text(100000))
    title = Column(String(1024))
    agenda = Column(Text())

Expected behavior
It should add module path automatically without raise KeyError

To Reproduce
Please try to provide a Minimal, Complete, and Verifiable example, with the migration script and/or the SQLAlchemy tables or models involved.
See also Reporting Bugs on the website.

    def test_render_create_table_with_user_module_type(self):
        class MyType(UserDefinedType):
            def get_col_spec(self):
                return "MYTYPE"

        type_ = MyType()
        uo = ops.UpgradeOps(
            ops=[
                ops.CreateTableOp(
                    "sometable", [Column("x", type_)]
                )
            ]
        )
        eq_(
            autogenerate.render_python_code(uo),
            "# ### commands auto generated by Alembic - please adjust! ###\n"
            "    op.create_table('sometable',\n"
            "    sa.Column('x', tests.test_autogen_render.MyType(), nullable=True)\n"
            "    )\n"
            "    # ### end Alembic commands ###",
        )

Error

Traceback (most recent call last): File "/home/tangkikodo/.pyenv-wsl/versions/3.7.8/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) 
File "/home/tangkikodo/.pyenv-wsl/versions/3.7.8/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) 
File "/home/tangkikodo/portal/portal/service/meeting_notes/models.py", line 51, in <module> create_orm_migration(Base.metadata) 
File "/home/tangkikodo/portal/portal/toolbox/ale_gen.py", line 20, in create_orm_migration ops.UpgradeOps(ops=operations), File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/api.py", line 203, in render_python_code render._render_cmd_body(up_or_down_op, autogen_context) 
File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/render.py", line 107, in _render_cmd_body lines = render_op(autogen_context, op) 
File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/render.py", line 125, in render_op lines = util.to_list(renderer(autogen_context, op)) 
File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/render.py", line 219, in _add_table _render_column(col, autogen_context) for col in table.columns 
File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/render.py", line 219, in <listcomp> _render_column(col, autogen_context) for col in table.columns 
File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/render.py", line 708, in _render_column "type": _repr_type(column.type, autogen_context), 
File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/render.py", line 838, in _repr_type prefix = _user_autogenerate_prefix(autogen_context, type_) 
File "/home/tangkikodo/portal/venv/lib/python3.7/site-packages/alembic/autogenerate/render.py", line 637, in _user_autogenerate_prefix prefix = autogen_context.opts["user_module_prefix"] KeyError: 'user_module_prefix'

Versions.

  • OS:
  • Python:
  • Alembic:
  • SQLAlchemy:
  • Database:
  • DBAPI:

Additional context

Have a nice day!

@allmonday allmonday added the requires triage New issue that requires categorization label May 5, 2023
@CaselIT CaselIT added bug Something isn't working autogenerate - rendering and removed requires triage New issue that requires categorization labels May 6, 2023
@CaselIT
Copy link
Member

CaselIT commented May 6, 2023

thanks! we will review the pr soon

@CaselIT CaselIT added this to the 1.11 milestone May 11, 2023
@sqla-tester
Copy link
Collaborator

tangkikodo has proposed a fix for this issue in the main branch:

set default None for user_module_prefix https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4609

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autogenerate - rendering bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants