-
-
Notifications
You must be signed in to change notification settings - Fork 630
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
Table 'XXX' is already defined for this MetaData instance #350
Comments
Similar problem when trying to set a timing decorator. Must be something general in the context of wrapper functions or decorators respectively. This is apperently not limited to SQLModel, since I'm not using SQLModel but declarative_base from SQLAlchemy. |
I cannot reproduce this error. Works fine for me. What version of SQLAlchemy is installed in the environment where you experienced this error? Or has this issue been resolved? |
I had the same problem. |
I have the same problem with even less code:
The same ERROR pops when I run the program with But everything looks OK if I run it with Here is pipenv graph, if it helps:
|
The problem is because from sqlmodel import Field, SQLModel
print("here")
class Hero(SQLModel, table=True): You can solve this by calling app = FastAPI()
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8001) Or, by separating the code into two files:
import uvicorn
if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=8001)
from typing import Optional
import uvicorn as uvicorn
from fastapi import FastAPI
from sqlmodel import Field, SQLModel
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
app = FastAPI() |
meirdev thanks issue resolved |
Thanks |
I was also getting this error when I imported the In the end I figured out that I was importing from |
Similar problem with streamlit |
this work for me. try adding this.
|
When the import path is incorrect, I encountered the following error: # occurred error
from foo.bar import Something
# worked out well
from src.foo.bar import Something |
I'm having the problem even without FastAPI:
By running Python version: |
Works with this edit to the
|
If you encounter such error, it might be because the file with models gets executed twice. Add a debug print on top of the file to check it. print("----") As mentioned above the cause of such behavior might be uvicorn. In my case it was relative imports. |
Thanks, that worked for me! |
First Check
Commit to Help
Example Code
Description
First attempt fails with.
Adding this fixes this problem, however reading various sites this is not recommended?
Second attempt fails with.
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateTable) relation "ix_hero_name" already exists [SQL: CREATE INDEX ix_hero_name ON hero (name)]
Moving the
create_db_and_tables()
out of the functions startup event fixes this problem and everything works as expectedAm I missing something that is causing this behavior?
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.10.4
Additional Context
No response
The text was updated successfully, but these errors were encountered: