Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cannot create and apply migrations #1225

Answered by YuriiMotov
TJPain asked this question in Questions
Discussion options

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_
You must be logged in to vote

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

Comment options

@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:

  1. Make changes to the model. models.py
  2. Run the migration steps as mentioned in the documentation . README.md
  3. Try to log in or make the API calls.

Following these steps should resolve the issue.

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
1 reply
Comment options

Or, simpler, you can install backend dependencies and run alembic revision --autogenerate -m "...." on your host machine (ensure db container is running)

Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested

AltStyle によって変換されたページ (->オリジナル) /