From 1c860b70e80e8b3f379c43c009e295e7f254ad13 Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Tue, 7 Jan 2025 15:25:01 +0100 Subject: [PATCH] [main] Slight changes to models to finally make them work --- src/models/__init__.py | 7 +++++-- src/models/author.py | 14 ++++++++------ src/models/base.py | 3 +++ src/models/book.py | 28 +++++++++++++--------------- src/models/book_category.py | 4 +--- src/models/book_category_link.py | 5 +---- src/models/librarian.py | 4 +--- src/models/loan.py | 4 +--- src/models/member.py | 5 +++-- 9 files changed, 36 insertions(+), 38 deletions(-) create mode 100644 src/models/base.py diff --git a/src/models/__init__.py b/src/models/__init__.py index dee725c..34e40bd 100644 --- a/src/models/__init__.py +++ b/src/models/__init__.py @@ -1,7 +1,10 @@ +from .base import Base from .author import Author -from .book_category import BookCategory from .book import Book +from .book_category import BookCategory from .book_category_link import BookCategoryLink from .member import Member from .librarian import Librarian -from .loan import Loan \ No newline at end of file +from .loan import Loan + +__all__ = ["Author", "Book", "BookCategory", "BookCategoryLink", "Member", "Librarian", "Loan"] diff --git a/src/models/author.py b/src/models/author.py index 40e56f8..93fdd45 100644 --- a/src/models/author.py +++ b/src/models/author.py @@ -1,15 +1,17 @@ from sqlalchemy import Column, Integer, String, TIMESTAMP, UniqueConstraint, func from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base -Base = declarative_base() +from .base import Base class Author(Base): - __tablename__ = 'author' + __tablename__ = 'author' __table_args__ = (UniqueConstraint('id'),) - id = Column(Integer, primary_key=True, autoincrement=True) - first_name = Column(String(50), nullable=False) - last_name = Column(String(50), nullable=False) + id = Column(Integer, primary_key=True, autoincrement=True) + first_name = Column(String(50), nullable=False) + last_name = Column(String(50), nullable=False) 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') diff --git a/src/models/base.py b/src/models/base.py new file mode 100644 index 0000000..860e542 --- /dev/null +++ b/src/models/base.py @@ -0,0 +1,3 @@ +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() diff --git a/src/models/book.py b/src/models/book.py index ce96965..6e86071 100644 --- a/src/models/book.py +++ b/src/models/book.py @@ -2,12 +2,8 @@ import enum from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base -from .author import Author -from .book_category import BookCategory - -Base = declarative_base() +from .base import Base class BookStatusEnum(enum.Enum): @@ -15,18 +11,20 @@ class BookStatusEnum(enum.Enum): borrowed = 'borrowed' reserved = 'reserved' + class Book(Base): - __tablename__ = 'book' + __tablename__ = 'book' __table_args__ = (UniqueConstraint('isbn'),) - id = Column(Integer, primary_key=True, autoincrement=True) - author_id = Column(Integer, ForeignKey('author.id'), nullable=False) - title = Column(String(100), nullable=False) - description = Column(Text, nullable=False) + id = Column(Integer, primary_key=True, autoincrement=True) + author_id = Column(Integer, ForeignKey('author.id'), nullable=False) + 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(BookStatusEnum), nullable=False, default=BookStatusEnum.available) - created_at = Column(TIMESTAMP, nullable=False, server_default=func.now()) - last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now()) + isbn = Column(String(13), nullable=False, unique=True) + status = Column(Enum(BookStatusEnum), nullable=False, default=BookStatusEnum.available) + created_at = 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') diff --git a/src/models/book_category.py b/src/models/book_category.py index d0d99eb..eb901a6 100644 --- a/src/models/book_category.py +++ b/src/models/book_category.py @@ -1,9 +1,7 @@ from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint, func from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base - -Base = declarative_base() +from .base import Base class BookCategory(Base): __tablename__ = 'book_category' diff --git a/src/models/book_category_link.py b/src/models/book_category_link.py index adde605..0bc52ad 100644 --- a/src/models/book_category_link.py +++ b/src/models/book_category_link.py @@ -1,12 +1,9 @@ from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base - from .book import Book from .book_category import BookCategory - -Base = declarative_base() +from .base import Base class BookCategoryLink(Base): __tablename__ = 'book_category_link' diff --git a/src/models/librarian.py b/src/models/librarian.py index 7a49364..019e067 100644 --- a/src/models/librarian.py +++ b/src/models/librarian.py @@ -2,10 +2,8 @@ import enum from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base - -Base = declarative_base() +from .base import Base class LibrarianStatusEnum(enum.Enum): diff --git a/src/models/loan.py b/src/models/loan.py index a82f922..2e34451 100644 --- a/src/models/loan.py +++ b/src/models/loan.py @@ -2,14 +2,12 @@ import enum from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, Float, func from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base +from .base import Base from .book import Book from .member import Member from .librarian import Librarian -Base = declarative_base() - class LoanStatusEnum(enum.Enum): borrowed = 'borrowed' diff --git a/src/models/member.py b/src/models/member.py index 9d9c97f..79ee7dc 100644 --- a/src/models/member.py +++ b/src/models/member.py @@ -2,13 +2,14 @@ import enum from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, func from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base + +from .base import Base + class MemberStatusEnum(enum.Enum): active = 'active' inactive = 'inactive' -Base = declarative_base() class Member(Base): __tablename__ = 'member'