Skip to content

Commit 7d3f242

Browse files
committed
Corrected various issues adding new books to database
1 parent 74ec4da commit 7d3f242

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

python-sqlite-sqlalchemy/project/examples/example_2/main.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"""
55

66
from importlib import resources
7+
78
from sqlalchemy import and_, create_engine
89
from sqlalchemy.orm import sessionmaker
910
from sqlalchemy.sql import asc, desc, func
10-
from treelib import Tree
1111

12-
from project.modules.models import Author, Book, Publisher
12+
from project.modules.models import Author, Book, Publisher, author_publisher
13+
from treelib import Tree
1314

1415

1516
def get_books_by_publishers(session, ascending=True):
@@ -87,14 +88,25 @@ def add_new_book(session, author_name, book_title, publisher_name):
8788
.filter(Book.publishers.any(Publisher.name == publisher_name))
8889
.one_or_none()
8990
)
90-
9191
# Does the book by the author and publisher already exist?
9292
if book is not None:
9393
return
94-
# Otherwise, create the book
95-
else:
94+
95+
# get the book by the author
96+
book = (
97+
session.query(Book)
98+
.join(Author)
99+
.filter(Book.title == book_title)
100+
.filter(
101+
and_(
102+
Author.first_name == first_name, Author.last_name == last_name
103+
)
104+
)
105+
.one_or_none()
106+
)
107+
# Create the new book if needed
108+
if book is None:
96109
book = Book(title=book_title)
97-
session.add(book)
98110

99111
# Get the author
100112
author = (
@@ -106,11 +118,15 @@ def add_new_book(session, author_name, book_title, publisher_name):
106118
)
107119
.one_or_none()
108120
)
109-
# Do we need to create the author
121+
# Do we need to create the author?
110122
if author is None:
111123
author = Author(first_name=first_name, last_name=last_name)
112124
session.add(author)
113125

126+
# Is this a new book for this author?
127+
if book not in [book_ for book_ in author.books]:
128+
author.books.append(book)
129+
114130
# Get the publisher
115131
publisher = (
116132
session.query(Publisher)
@@ -122,14 +138,10 @@ def add_new_book(session, author_name, book_title, publisher_name):
122138
publisher = Publisher(name=publisher_name)
123139
session.add(publisher)
124140

125-
# Add the book to the author's books collection
126-
author.books.append(book)
127-
128-
# Add the book to the publisher's books collection
129-
publisher.books.append(book)
130-
131-
# Add the publisher to the author's publishers collection
132-
author.publishers.append(publisher)
141+
# Initialize the book relationships
142+
book.author = author
143+
book.publishers.append(publisher)
144+
session.add(book)
133145

134146
# Commit to the database
135147
session.commit()

python-sqlite-sqlalchemy/project/modules/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class Author(Base):
3030
publishers = relationship(
3131
"Publisher", secondary=author_publisher, back_populates="authors"
3232
)
33+
def __eq__(self, other):
34+
author_name = f"{self.first_name} {self.last_name}"
35+
other_name = f"{other.first_name} {other.last_name}"
36+
return author_name == other_name
3337

3438

3539
class Book(Base):
@@ -40,6 +44,8 @@ class Book(Base):
4044
publishers = relationship(
4145
"Publisher", secondary=book_publisher, back_populates="books"
4246
)
47+
def __eq__(self, other):
48+
return self.title == other.title
4349

4450

4551
class Publisher(Base):
@@ -52,3 +58,6 @@ class Publisher(Base):
5258
books = relationship(
5359
"Book", secondary=book_publisher, back_populates="publishers"
5460
)
61+
def __eq__(self, other):
62+
return self.name == other.name
63+

0 commit comments

Comments
 (0)