Skip to content

Alembic migration generated always set enum nullable to true #466

Description

@yixiongngvsys

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import enum
from sqlmodel import SQLModel, Field, UniqueConstraint, Column, Enum

class Contract_Status(str, enum.Enum):
    CREATED = "Created"
    COMPLETED = "Completed"
    DEPOSITED = "Deposited"
    CANCELLED = "Cancelled"

class Contract(SQLModel, table = True):
    __table_args__ = (UniqueConstraint("ctrt_id"),)
    ctrt_id: str = Field(primary_key = True, nullable = False)
    status: Contract_Status = Field(sa_column = Column(Enum(Contract_Status, values_callable = lambda enum: [e.value for e in enum])), nullable = False)

Description

  1. Create Contract Model
  2. Create migration using alembic
    alembic revision --autogenerate -m "MIGRATION MESSAGE"
  3. Migration generated, however, nullable for enum is always set to True

Note: I have tested using sqlalchemy defination, and it is working but not for SQLModel.

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('contract',
    sa.Column('status', sa.Enum('Created', 'Completed', 'Deposited', 'Cancelled', name='contract_status'), nullable=True),
    sa.Column('ctrt_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.PrimaryKeyConstraint('ctrt_id'),
    sa.UniqueConstraint('ctrt_id')
    )
    # ### end Alembic commands ###

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.8

Python Version

3.10.5

Additional Context

No response

Activity

  1. bvsn commented on Oct 20, 2022

    @bvsn

    You can reuse ChoiceType and set server_default in your migration file:

    sa.Column("my_field", ChoiceType(MyFieldEnum), nullable=False, server_default=str(MyFieldEnum.MY_X.value))
    

    Or, just to rewrite a single ChoiceType realization.

  2. yixiongngvsys commented on Oct 25, 2022

    @yixiongngvsys
    Author

    Thanks for your reply @bvsn however my plan is to make it working from model.py files and try to reduce customisation in migration files.

  3. tiangolo commented on Oct 29, 2023

    @tiangolo
    Member

    Thanks for the report! This is because nullable is not compatible with sa_column, in that case you have to pass nullable to it.

    I just added #681 to make sure that that combination of parameters is not allowed. When sa_column is provided, that takes precedence. Now it will show an error when that happens, the error will happen at runtime, and there's also the type tricks so that your editor will also complain if the combination of parameters is invalid.

    Also, there was recently a fix for enums, so you should be able to use the enum directly, without a Column. 🎉

  4. github-actions commented on Nov 9, 2023

    @github-actions
    Contributor

    Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions