Compare commits
2 Commits
765b939998
...
5dde0468d0
Author | SHA1 | Date | |
---|---|---|---|
5dde0468d0 | |||
cd18b3ab46 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -160,3 +160,4 @@ cython_debug/
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
*.bak
|
||||
|
@ -1,12 +1,17 @@
|
||||
from sqlalchemy import Column, Integer, VARCHAR
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
from .book import Book
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
class Author(Base):
|
||||
__tablename__ = "author"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
first_name = Column(VARCHAR(length=50))
|
||||
last_name = Column(VARCHAR(length=50))
|
||||
class Author(Base):
|
||||
__tablename__ = 'author'
|
||||
|
||||
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
28
src/models/book.py
Normal 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')
|
19
src/models/book_category.py
Normal file
19
src/models/book_category.py
Normal 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')
|
10
src/models/book_category_link.py
Normal file
10
src/models/book_category_link.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user