Skip to content
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

Alembic migration generated always set enum nullable to true #466

Closed
8 tasks done
yixiongngvsys opened this issue Oct 11, 2022 · 4 comments
Closed
8 tasks done

Alembic migration generated always set enum nullable to true #466

yixiongngvsys opened this issue Oct 11, 2022 · 4 comments
Labels
answered investigate question Further information is requested

Comments

@yixiongngvsys
Copy link

yixiongngvsys commented Oct 11, 2022

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

@bvsn
Copy link

bvsn commented Oct 20, 2022

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.

@yixiongngvsys
Copy link
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.

@tiangolo
Copy link
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. 🎉

Copy link

github-actions bot commented Nov 9, 2023

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.

@github-actions github-actions bot closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered investigate question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants