[main] Updated models and their initialization

This commit is contained in:
Thastertyn 2025-01-07 00:18:02 +01:00
parent 1e36c2d966
commit 76465636ee
7 changed files with 28 additions and 24 deletions

View File

@ -0,0 +1,7 @@
from .author import Author
from .book_category import BookCategory
from .book import Book
from .book_category_link import BookCategoryLink
from .member import Member
from .librarian import Librarian
from .loan import Loan

View File

@ -1,9 +1,7 @@
from sqlalchemy import Column, Integer, String, TIMESTAMP, UniqueConstraint from sqlalchemy import Column, Integer, String, TIMESTAMP, UniqueConstraint, func
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()
@ -11,7 +9,7 @@ class Author(Base):
__tablename__ = 'author' __tablename__ = 'author'
__table_args__ = (UniqueConstraint('id'),) __table_args__ = (UniqueConstraint('id'),)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String(50), nullable=False) first_name = Column(String(50), nullable=False)
last_name = Column(String(50), nullable=False) last_name = Column(String(50), nullable=False)
last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())

View File

@ -1,6 +1,6 @@
import enum import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -19,14 +19,14 @@ class Book(Base):
__tablename__ = 'book' __tablename__ = 'book'
__table_args__ = (UniqueConstraint('isbn'),) __table_args__ = (UniqueConstraint('isbn'),)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True, autoincrement=True)
author_id = Column(Integer, ForeignKey('author.id'), nullable=False) author_id = Column(Integer, ForeignKey('author.id'), nullable=False)
title = Column(String(100), nullable=False) title = Column(String(100), nullable=False)
description = Column(Text, nullable=False) description = Column(Text, nullable=False)
year_published = Column(String(4), nullable=False) year_published = Column(String(4), nullable=False)
isbn = Column(String(13), nullable=False, unique=True) isbn = Column(String(13), nullable=False, unique=True)
status = Column(Enum(BookStatusEnum), nullable=False, default=BookStatusEnum.available) status = Column(Enum(BookStatusEnum), nullable=False, default=BookStatusEnum.available)
created_at = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())
last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())
author = relationship('Author', backref='books') author = relationship('Author', backref='books')

View File

@ -1,5 +1,4 @@
from .book import Book from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint, func
from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -10,10 +9,10 @@ class BookCategory(Base):
__tablename__ = 'book_category' __tablename__ = 'book_category'
__table_args__ = (UniqueConstraint('name'),) __table_args__ = (UniqueConstraint('name'),)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True, autoincrement=True)
parent_category_id = Column(Integer, ForeignKey('book_category.id'), nullable=True) parent_category_id = Column(Integer, ForeignKey('book_category.id'), nullable=True)
name = Column(String(100), nullable=False) name = Column(String(100), nullable=False)
mature_content = Column(Integer, nullable=False, default=0) mature_content = Column(Integer, nullable=False, default=0)
last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())
parent_category = relationship('BookCategory', remote_side=[id]) parent_category = relationship('BookCategory', remote_side=[id])

View File

@ -1,6 +1,6 @@
import enum import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -21,12 +21,12 @@ class Librarian(Base):
__tablename__ = 'librarian' __tablename__ = 'librarian'
__table_args__ = (UniqueConstraint('id'),) __table_args__ = (UniqueConstraint('id'),)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String(50), nullable=False) first_name = Column(String(50), nullable=False)
last_name = Column(String(50), nullable=False) last_name = Column(String(50), nullable=False)
email = Column(String(100), nullable=False, unique=True) email = Column(String(100), nullable=False, unique=True)
phone = Column(String(20), nullable=False) phone = Column(String(20), nullable=False)
hire_date = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') hire_date = Column(TIMESTAMP, nullable=False, server_default=func.now())
status = Column(Enum(LibrarianStatusEnum), nullable=False, default=LibrarianStatusEnum.active) status = Column(Enum(LibrarianStatusEnum), nullable=False, default=LibrarianStatusEnum.active)
role = Column(Enum(LibrarianRoleEnum), nullable=False) role = Column(Enum(LibrarianRoleEnum), nullable=False)
last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())

View File

@ -1,6 +1,6 @@
import enum import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, Float from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, Float, func
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -22,16 +22,16 @@ class Loan(Base):
__tablename__ = 'loan' __tablename__ = 'loan'
__table_args__ = (UniqueConstraint('id'),) __table_args__ = (UniqueConstraint('id'),)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True, autoincrement=True)
book_id = Column(Integer, ForeignKey('book.id'), nullable=False) book_id = Column(Integer, ForeignKey('book.id'), nullable=False)
member_id = Column(Integer, ForeignKey('member.id'), nullable=False) member_id = Column(Integer, ForeignKey('member.id'), nullable=False)
librarian_id = Column(Integer, ForeignKey('librarian.id'), nullable=False) librarian_id = Column(Integer, ForeignKey('librarian.id'), nullable=False)
loan_date = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') loan_date = Column(TIMESTAMP, nullable=False, server_default=func.now())
due_date = Column(TIMESTAMP, nullable=False) due_date = Column(TIMESTAMP, nullable=False)
return_date = Column(TIMESTAMP, nullable=True) return_date = Column(TIMESTAMP, nullable=True)
status = Column(Enum(LoanStatusEnum), nullable=False, default=LoanStatusEnum.borrowed) status = Column(Enum(LoanStatusEnum), nullable=False, default=LoanStatusEnum.borrowed)
overdue_fee = Column(Float, nullable=True) overdue_fee = Column(Float, nullable=True)
last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())
book = relationship('Book', backref='loans') book = relationship('Book', backref='loans')
member = relationship('Member', backref='loans') member = relationship('Member', backref='loans')

View File

@ -1,6 +1,6 @@
import enum import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -14,11 +14,11 @@ class Member(Base):
__tablename__ = 'member' __tablename__ = 'member'
__table_args__ = (UniqueConstraint('id'),) __table_args__ = (UniqueConstraint('id'),)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String(50), nullable=False) first_name = Column(String(50), nullable=False)
last_name = Column(String(50), nullable=False) last_name = Column(String(50), nullable=False)
email = Column(String(100), nullable=False, unique=True) email = Column(String(100), nullable=False, unique=True)
phone = Column(String(20), nullable=True) phone = Column(String(20), nullable=True)
register_date = Column(TIMESTAMP, nullable=True, default='CURRENT_TIMESTAMP()') register_date = Column(TIMESTAMP, nullable=True, server_default=func.now())
status = Column(Enum(MemberStatusEnum), nullable=True, default=MemberStatusEnum.active) status = Column(Enum(MemberStatusEnum), nullable=True, default=MemberStatusEnum.active)
last_updated = Column(TIMESTAMP, nullable=True) last_updated = Column(TIMESTAMP, nullable=True)