database.pyfrom sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "postgresql+psycopg2://postgres:123@localhost/cms"
#SQLALCHEMY_DATABASE_URL = "mysql://root@localhost/cms"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
models.pyfrom sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from .database import Base
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
email = Column(String(15), unique=True, index=True)
hashed_password = Column(String(15))
is_active = Column(Boolean, default=True)
items = relationship("Item", back_populates="owner")
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
title = Column(String(15), index=True)
description = Column(String(15), index=True)
owner_id = Column(Integer, ForeignKey("
users.id"))
owner = relationship("User", back_populates="items")
db.pyfrom sql_app import models
from sql_app.database import engine
def init_db():
models.Base.metadata.create_all(bind=engine)
init_db()