Hello there, i’m trying to setup a project with Streamlit and SQLModel, it’ is working as expected to query/create tables with Alembic but when I try to reload my page pressing R or reloading the browser this exception appear:
sqlalchemy.exc.InvalidRequestError: Table 'usuarios' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
Traceback:
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
result = func()
^^^^^^
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec
exec(code, module.__dict__)
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\main.py", line 7, in <module>
from models.Usuarios import Usuarios
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\models\Usuarios.py", line 4, in <module>
class Usuarios(SQLModel, table=True, ):
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlmodel\main.py", line 638, in __init__
DeclarativeMeta.__init__(cls, classname, bases, dict_, **kw)
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\orm\decl_api.py", line 196, in __init__
_as_declarative(reg, cls, dict_)
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 244, in _as_declarative
return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 325, in setup_mapping
return _ClassScanMapperConfig(
^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 576, in __init__
self._setup_table(table)
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 1759, in _setup_table
table_cls(
File "<string>", line 2, in __new__
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\util\deprecations.py", line 281, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\sql\schema.py", line 427, in __new__
return cls._new(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^
File "E:\Desenvolvimento\Trabalho-MyIA\myiabr\venv\Lib\site-packages\sqlalchemy\sql\schema.py", line 459, in _new
raise exc.InvalidRequestError(
I’m using Python 3.11.2 and Streamlit 1.41.1 and this is my following code for getting the engine:
from sqlmodel import create_engine
from dotenv import load_dotenv
import os
import streamlit as st
@st.cache_resource
def get_engine():
load_dotenv()
DATABASE = os.getenv("DATABASE")
USERNAME = os.getenv("USER")
PASSWORD = os.getenv("PASSWORD")
HOST = os.getenv("HOST")
PORT = os.getenv("PORT")
DATABASE_URL = f"postgresql+psycopg2://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}"
print(f"ENGINE CREATED: {DATABASE_URL}")
return create_engine(DATABASE_URL)
engine = get_engine()
And currently i’m working with only one table, and there it is:
from sqlmodel import SQLModel, Field, Relationship
from typing import Optional
class Usuarios(SQLModel, table=True, ):
id: Optional[int] = Field(default=None, primary_key=True, )
login: str
nome: str
senha: str
I already tried to use extend_existing=True but it’s doesn’t work, it even accept this argument in the Class definition or column.
I cannot share my whole code because it’s protected by the company i’m working for, so if someone knows anything that can help please share below! By the way, i’m not a english native speaker, so sorry if there’s grammar mistakes.