[main] Initial code for SqlAlchemy and it's models

This commit is contained in:
Thastertyn 2025-01-05 19:30:42 +01:00
parent 765b939998
commit cd18b3ab46
4 changed files with 68 additions and 6 deletions

View File

@ -1,12 +1,17 @@
from sqlalchemy import Column, Integer, VARCHAR from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from .book import Book
Base = declarative_base() Base = declarative_base()
class Author(Base):
__tablename__ = "author"
id = Column(Integer, primary_key=True) class Author(Base):
first_name = Column(VARCHAR(length=50)) __tablename__ = 'author'
last_name = Column(VARCHAR(length=50))
id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String(50), nullable=False)
last_name = Column(String(50), nullable=False)
books = relationship('Book', back_populates='author')

28
src/models/book.py Normal file
View File

@ -0,0 +1,28 @@
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from .author import Author
from .book_category import BookCategory
Base = declarative_base()
class Book(Base):
__tablename__ = 'book'
id = Column(Integer, primary_key=True, autoincrement=True)
author_id = Column(Integer, ForeignKey(
'author.id'), nullable=False, index=True)
title = Column(String(100), nullable=False)
description = Column(Text, nullable=False)
year_published = Column(String(4), nullable=False)
isbn = Column(String(13), nullable=False, unique=True)
status = Column(Enum('available', 'borrowed', 'reserved'),
nullable=False, default='available')
created_at = Column(TIMESTAMP, nullable=True, default="CURRENT_TIMESTAMP")
author = relationship('Author', back_populates='books')
# loans = relationship('Loan', back_populates='book')
categories = relationship(
'BookCategory', secondary='book_category_link', back_populates='books')

View File

@ -0,0 +1,19 @@
from .book import Book
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class BookCategory(Base):
__tablename__ = 'book_category'
id = Column(Integer, primary_key=True, autoincrement=True)
parent_category_id = Column(Integer, ForeignKey(
'book_category.id'), nullable=True, index=True)
name = Column(String(100), nullable=False, unique=True)
parent_category = relationship('BookCategory', remote_side=[id])
books = relationship(
'Book', secondary='book_category_link', back_populates='categories')

View File

@ -0,0 +1,10 @@
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class BookCategoryLink(Base):
__tablename__ = 'book_category_link'
book_id = Column(Integer, ForeignKey('book.id'), primary_key=True, index=True)
book_category_id = Column(Integer, ForeignKey('book_category.id'), primary_key=True, index=True)