diff --git a/src/models/__init__.py b/src/models/__init__.py index e69de29..dee725c 100644 --- a/src/models/__init__.py +++ b/src/models/__init__.py @@ -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 \ No newline at end of file diff --git a/src/models/author.py b/src/models/author.py index 4c24543..40e56f8 100644 --- a/src/models/author.py +++ b/src/models/author.py @@ -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.ext.declarative import declarative_base -from .book import Book - Base = declarative_base() @@ -11,7 +9,7 @@ class Author(Base): __tablename__ = 'author' __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) 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()) diff --git a/src/models/book.py b/src/models/book.py index 219c43c..ce96965 100644 --- a/src/models/book.py +++ b/src/models/book.py @@ -1,6 +1,6 @@ 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.ext.declarative import declarative_base @@ -19,14 +19,14 @@ class Book(Base): __tablename__ = 'book' __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) 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, default='CURRENT_TIMESTAMP()') - last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') + 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') diff --git a/src/models/book_category.py b/src/models/book_category.py index 939e844..d0d99eb 100644 --- a/src/models/book_category.py +++ b/src/models/book_category.py @@ -1,5 +1,4 @@ -from .book import Book -from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint +from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint, func from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base @@ -10,10 +9,10 @@ class BookCategory(Base): __tablename__ = 'book_category' __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) name = Column(String(100), nullable=False) 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]) diff --git a/src/models/librarian.py b/src/models/librarian.py index 06c21e2..7a49364 100644 --- a/src/models/librarian.py +++ b/src/models/librarian.py @@ -1,6 +1,6 @@ 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.ext.declarative import declarative_base @@ -21,12 +21,12 @@ class Librarian(Base): __tablename__ = 'librarian' __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) last_name = Column(String(50), nullable=False) email = Column(String(100), nullable=False, unique=True) 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) 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()) diff --git a/src/models/loan.py b/src/models/loan.py index 3b2d976..a82f922 100644 --- a/src/models/loan.py +++ b/src/models/loan.py @@ -1,6 +1,6 @@ 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.ext.declarative import declarative_base @@ -22,16 +22,16 @@ class Loan(Base): __tablename__ = 'loan' __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) member_id = Column(Integer, ForeignKey('member.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) return_date = Column(TIMESTAMP, nullable=True) status = Column(Enum(LoanStatusEnum), nullable=False, default=LoanStatusEnum.borrowed) 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') member = relationship('Member', backref='loans') diff --git a/src/models/member.py b/src/models/member.py index e733e01..9d9c97f 100644 --- a/src/models/member.py +++ b/src/models/member.py @@ -1,6 +1,6 @@ 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.ext.declarative import declarative_base @@ -14,11 +14,11 @@ class Member(Base): __tablename__ = 'member' __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) last_name = Column(String(50), nullable=False) email = Column(String(100), nullable=False, unique=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) last_updated = Column(TIMESTAMP, nullable=True) \ No newline at end of file