[main] Slight changes to models to finally make them work

This commit is contained in:
Thastertyn 2025-01-07 15:25:01 +01:00
parent b22a0bb664
commit 1c860b70e8
9 changed files with 36 additions and 38 deletions

View File

@ -1,7 +1,10 @@
from .base import Base
from .author import Author from .author import Author
from .book_category import BookCategory
from .book import Book from .book import Book
from .book_category import BookCategory
from .book_category_link import BookCategoryLink from .book_category_link import BookCategoryLink
from .member import Member from .member import Member
from .librarian import Librarian from .librarian import Librarian
from .loan import Loan from .loan import Loan
__all__ = ["Author", "Book", "BookCategory", "BookCategoryLink", "Member", "Librarian", "Loan"]

View File

@ -1,15 +1,17 @@
from sqlalchemy import Column, Integer, String, TIMESTAMP, UniqueConstraint, func 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
Base = declarative_base() from .base import Base
class Author(Base): class Author(Base):
__tablename__ = 'author' __tablename__ = 'author'
__table_args__ = (UniqueConstraint('id'),) __table_args__ = (UniqueConstraint('id'),)
id = Column(Integer, primary_key=True, autoincrement=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, server_default=func.now()) last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())
# Reference 'Book' as a string to avoid direct import
books = relationship('Book', back_populates='author')

3
src/models/base.py Normal file
View File

@ -0,0 +1,3 @@
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

View File

@ -2,12 +2,8 @@ import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func 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 .author import Author from .base import Base
from .book_category import BookCategory
Base = declarative_base()
class BookStatusEnum(enum.Enum): class BookStatusEnum(enum.Enum):
@ -15,18 +11,20 @@ class BookStatusEnum(enum.Enum):
borrowed = 'borrowed' borrowed = 'borrowed'
reserved = 'reserved' reserved = 'reserved'
class Book(Base): class Book(Base):
__tablename__ = 'book' __tablename__ = 'book'
__table_args__ = (UniqueConstraint('isbn'),) __table_args__ = (UniqueConstraint('isbn'),)
id = Column(Integer, primary_key=True, autoincrement=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, server_default=func.now()) created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())
last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now()) last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())
author = relationship('Author', backref='books') # Reference 'Author' as a string to avoid direct import
author = relationship('Author', back_populates='books')

View File

@ -1,9 +1,7 @@
from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint, func from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint, func
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from .base import Base
class BookCategory(Base): class BookCategory(Base):
__tablename__ = 'book_category' __tablename__ = 'book_category'

View File

@ -1,12 +1,9 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from .book import Book from .book import Book
from .book_category import BookCategory from .book_category import BookCategory
from .base import Base
Base = declarative_base()
class BookCategoryLink(Base): class BookCategoryLink(Base):
__tablename__ = 'book_category_link' __tablename__ = 'book_category_link'

View File

@ -2,10 +2,8 @@ import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func 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 .base import Base
Base = declarative_base()
class LibrarianStatusEnum(enum.Enum): class LibrarianStatusEnum(enum.Enum):

View File

@ -2,14 +2,12 @@ import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, Float, func 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 .base import Base
from .book import Book from .book import Book
from .member import Member from .member import Member
from .librarian import Librarian from .librarian import Librarian
Base = declarative_base()
class LoanStatusEnum(enum.Enum): class LoanStatusEnum(enum.Enum):
borrowed = 'borrowed' borrowed = 'borrowed'

View File

@ -2,13 +2,14 @@ import enum
from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func 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 .base import Base
class MemberStatusEnum(enum.Enum): class MemberStatusEnum(enum.Enum):
active = 'active' active = 'active'
inactive = 'inactive' inactive = 'inactive'
Base = declarative_base()
class Member(Base): class Member(Base):
__tablename__ = 'member' __tablename__ = 'member'