#SQLAlchemy and FastAPI

20 messages · Page 1 of 1 (latest)

valid anvil
#

Hi, I have my file structure like so:

-folder
   - main.py
   - database.py
   - models.py
   - api.py
   - __init__.py

And files like so:
main.py

import os

from fastapi import FastAPI

from api import API
from database import engine, Base

if not os.path.isfile("./../tickets.db"):
    Base.metadata.create_all(bind=engine)

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


app.include_router(API, prefix='/api')

database.py

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///../tickets.db"

engine = create_engine(
    SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)

Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

models.py

from sqlalchemy import Column, ForeignKey, Integer, String, DateTime, func
from sqlalchemy.orm import relationship

from database import Base


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, autoincrement=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String, nullable=False)

    tickets = relationship("Ticket", back_populates="owner")


class Ticket(Base):
    __tablename__ = 'tickets'

    id = Column(Integer, primary_key=True, autoincrement=True)
    account_id = Column(Integer, ForeignKey("users.id"))
    purchased = Column(DateTime(timezone=True), server_default=func.now())
    expiry = Column(Integer, nullable=False)

    owner = relationship("User", back_populates="tickets")

#

Error I'm facing: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: tickets [SQL: INSERT INTO tickets (account_id, expiry) VALUES (?, ?) RETURNING id, purchased] [parameters: (1, 30)]
Even tho I have specified the schema, the .db file has nothing in it, even no schema, which is weird.

I can specify my problem if something is not clear enough.

signal edge
#

When you run it, does the empty tickets.db exist?

valid anvil
#

yes

#

but vscode doesn't throw that warning you get before you try to open it in code editor

#

it's like blank txt file right now

#

like it feels like it can't reach my database schemas (models.py)

signal edge
#

Well your code won't rebuild the db if the file exists

#

So if the file is empty it won't run the code to create the schemas

valid anvil
#

I deleted it manually to test that theory

signal edge
#

Are you importing your models.py anywhere??? They have to be imported for them to be attached to the declarative base

valid anvil
#

idk what is just did, but it started to work with modular approach

#

thank you for helping tho

signal edge
#

Probably didn't have the models.py being imported before the create_all call

valid anvil
#

I deleted and added some stuff by moving the contents of models to database.py file

#

but it doesn't show up on git that anything changed with that import

#

really weird

#

gotta be the bit flip from space

#

or smh