sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.61k stars 234 forks source link

Migrations File don't auto import sqlalchemy_utils #1405

Closed ZackPlauche closed 5 months ago

ZackPlauche commented 5 months ago

Describe the bug

I'm working on an SQLAlchemy backend project. I have a JSONType from sqlalchemy_utils. It's installed in my venv. My code works everywhere else...

sqlalchemy_utils is not appearing in the migrations file as an import for some reason.

Please tell me how to fix this. Expected behavior

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. Use sqlalchemy_utils JSONType in a field and try to migrate 🤷‍♂️

# Insert code here
from sqlalchemy_utils import JSONType

class Invoice(Base):
    __tablename__ = 'invoices'

    id = Column(Integer, primary_key=True)
    amount = Column(Integer, nullable=False, comment='Amount in cents. Assumes USD.')
    created_at = Column(DateTime, nullable=False)
    platform = Column(String(50), nullable=False)
    platform_id = Column(String(50), nullable=False, unique=True, comment='The id of the invoice on the platform.')
    status = Column(String(50), nullable=False, comment='The status of the invoice on the platform.')
    data = Column(JSONType, nullable=True, comment='The raw data from the platform.')

Error

# Copy error here. Please include the full stack trace.
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
ERROR [alembic.util.messaging] Target database is not up to date.
FAILED: Target database is not up to date.
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 0c0645c7a7ab, Update database
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Scripts\alembic.exe\__main__.py", line 7, in <module>
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Lib\site-packages\alembic\config.py", line 641, in main
    CommandLine(prog=prog).main(argv=argv)
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Lib\site-packages\alembic\config.py", line 631, in main
    self.run_cmd(cfg, options)
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Lib\site-packages\alembic\config.py", line 608, in run_cmd
    fn(
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Lib\site-packages\alembic\command.py", line 403, in upgrade  
    script.run_env()
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Lib\site-packages\alembic\script\base.py", line 583, in run_env
_module_py
    spec.loader.exec_module(module)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module 
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\zackp\Desktop\zackcrm\backend\crm/database/migrations\env.py", line 84, in <module>
    run_migrations_online()
  File "C:\Users\zackp\Desktop\zackcrm\backend\crm/database/migrations\env.py", line 78, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Lib\site-packages\alembic\runtime\environment.py", line 948, in run_migrations
    self.get_context().run_migrations(**kw)
  File "C:\Users\zackp\.virtualenvs\zackcrm-99W6RUPI\Lib\site-packages\alembic\runtime\migration.py", line 627, in run_migrations
    step.migration_fn(**kw)
  File "C:\Users\zackp\Desktop\zackcrm\backend\crm\database\migrations\versions\0c0645c7a7ab_update_database.py", line 34, in upgrade
    batch_op.add_column(sa.Column('data', sqlalchemy_utils.types.json.JSONType(), nullable=True, comment='The raw data from the platform.'))        
                                          ^^^^^^^^^^^^^^^^
NameError: name 'sqlalchemy_utils' is not defined

Versions.

Additional context

Have a nice day!