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
classFoo(SQLModel, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
bar: str
Description
Create model, with id field as described in docs
Run alembic migrations
Upgrade head
Run alembic migrations again
Alembic reads the primary key as nullable.
When running second time it tries to alter the field
You will then get an error when trying to upgrade.
I using psycopg2-binary
*Adding nullable=False to id declaration fixes this. Should this be added to the tutorial?
For anybody facing issues with alembic and sqlmodel.
People wrote issues about migrations in very first days of this project I believe, you are not alone ;)
My own investigation has shown that (alembic migrations) they are completely broken (even typo errors - indexes, see PR #11, simple but not yet merged) at the moment and although we see 0.0.4, it should be still alpha, beta or pre-release w/e, it's just not yet ready for something more than "hello world" playground and hopefully tiangolo will find time to continue looking into this project, it looks very promising.
So the answer to the main question: Probably a bug
yes, i got the same problem, alembic was trying to alter column ID even when is a primary key. the way i ommited the error was to remove any reference of alter column where id was modified in the migration file.
Not so much a real issue if you can read and tweak alembic's autogen code... but if u wanna keep things idiot proof, which is usually the case, gotta get alembic workin like magic
Activity
rafsaf commented on Oct 19, 2021
For anybody facing issues with alembic and sqlmodel.
People wrote issues about migrations in very first days of this project I believe, you are not alone ;)
My own investigation has shown that (alembic migrations) they are completely broken (even typo errors - indexes, see PR #11, simple but not yet merged) at the moment and although we see 0.0.4, it should be still alpha, beta or pre-release w/e, it's just not yet ready for something more than "hello world" playground and hopefully tiangolo will find time to continue looking into this project, it looks very promising.
So the answer to the main question: Probably a bug
jdgaravito commented on Oct 24, 2021
yes, i got the same problem, alembic was trying to alter column ID even when is a primary key. the way i ommited the error was to remove any reference of alter column where id was modified in the migration file.
heyaco commented on Dec 28, 2021
Gotta get alembic working. Having the same issue.
wangxin688 commented on Jan 24, 2022
facing the same issue.
heyaco commented on Jan 24, 2022
Not so much a real issue if you can read and tweak alembic's autogen code... but if u wanna keep things idiot proof, which is usually the case, gotta get alembic workin like magic
Naisisor commented on Feb 7, 2022
facing the same issue.
thomasborgen commented on Mar 4, 2022
Same issue
murata100 commented on Mar 7, 2022
I did it like this.
If primary_key=True, be sure to specify nullable=False
Before
class Foo(SQLModel, table=True): id: Optional[int] = Field(primary_key=True) bar: strAfter
class Foo(SQLModel, table=True): id: Optional[int] = Field(primary_key=True, nullable=False) bar: strThis works fine and probably works as desired before and after the alembic bug has been fixed.
Please let me know if you have any problems with this
alexberezin commented on Mar 9, 2022
Worked for me
tiangolo commented on Aug 27, 2022
Thanks for the report and for the discussion everyone! This was probably fixed in #79
It will be available in SQLModel 0.0.7, released in the next hours. 🚀
github-actions commented on Sep 7, 2022
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.