-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Cannot create and apply migrations #1225
-
First Check
- I added a very descriptive title here.
- I used the GitHub search to find a similar question and didn't find it.
- I searched in the documentation/README.
- I already searched in Google "How to do X" and didn't find any information.
- I already read and followed all the tutorial in the docs/README and didn't find an answer.
Commit to Help
- I commit to help with one of those options 👆
Example Code
I have this issue when I make any change to a model e.g from:
class UserBase(SQLModel):
email: str = Field(unique=True, index=True)
is_active: bool = True
is_superuser: bool = False
full_name: str | None = None
Changed to:
class UserBase(SQLModel):
email: str = Field(unique=True, index=True)
is_active: bool = True
is_superuser: bool = False
first_name: str | None = None
last_name: str | None = None
So a very simple change, splitting full_name into first_name and last_name
Description
I have downloaded and setup the project and it runs well in docker and locally for the frontend. However, when I make changes to a model, e.g. removing the UserBase full_name column and adding first_name and last_name columns, the backend service then stops running with
2024年06月06日 22:13:41 LINE 1: ...er".email, "user".is_active, "user".is_superuser, "user".fir...
2024年06月06日 22:13:41 ^
2024年06月06日 22:13:41 [SQL: SELECT "user".email, "user".is_active, "user".is_superuser, "user".first_name, "user".last_name, "user".id, "user".hashed_password
2024年06月06日 22:13:41 FROM "user"
2024年06月06日 22:13:41 WHERE "user".email = %(email_1)s::VARCHAR]
2024年06月06日 22:13:41 [parameters: {'email_1': 'admin@example.com'}]
2024年06月06日 22:13:41 (Background on this error at: https://sqlalche.me/e/20/f405)```
I therefore cannot autogenerate or apply the migration, which I believe is the correct workflow. Am I missing something very obvious here?
### Operating System
macOS
### Operating System Details
macOS Sonoma 14.1.1
### Python Version
3.10
### Additional Context
_No response_
Beta Was this translation helpful? Give feedback.
All reactions
You need to start backend container without starting the fastapi app, then run alembic revision --autogenerate -m "...." and alembic upgrade head (as described in the docs).
Then you can start fastapi app.
See this answer to make backend container run without running fastapi app
Replies: 2 comments 1 reply
-
@TJPain I believe this issue might be coincidental, and there are two potential scenarios to consider:
Scenario One: You are running the frontend and attempting to log in after making changes to the model file, but you have not performed any database migration.
Scenario Two: You are running both the frontend and backend, and you already logged into client using the admin email, as indicated in the error message you mentioned. Frontend making calls to backend to verify logged in identity
The error you are encountering is not directly related to updating the model but occurs when making an API call to retrieve user details without migrating changes to DB. This can happen either from the frontend or through the Swagger documentation.
Suggestion / Correct Sequence of Actions:
- Make changes to the model. models.py
- Run the migration steps as mentioned in the documentation . README.md
- Try to log in or make the API calls.
Following these steps should resolve the issue.
Beta Was this translation helpful? Give feedback.
All reactions
-
You need to start backend container without starting the fastapi app, then run alembic revision --autogenerate -m "...." and alembic upgrade head (as described in the docs).
Then you can start fastapi app.
See this answer to make backend container run without running fastapi app
Beta Was this translation helpful? Give feedback.
All reactions
-
Or, simpler, you can install backend dependencies and run alembic revision --autogenerate -m "...." on your host machine (ensure db container is running)
Beta Was this translation helpful? Give feedback.