How to get all books from an author using flask SQLAlchemy?

Question:

So I’ve setup two models so far:

class BookModel(db.Model):
    __tablename__ = "books"

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), unique=False, nullable=False)
    author_first_name = db.Column(db.Integer, db.ForeignKey('authors.id'))
    author_last_name = db.Column(db.Integer, db.ForeignKey('authors.id'))
    isbn = db.Column(db.String(17), unique=True, nullable=False)

class AuthorModel(db.Model):
    __tablename__ = "authors"

    id = db.Column(db.Integer, primary_key=True)
    author_first_name = db.Column(db.String(80), unique=False, nullable=False)
    author_last_name = db.Column(db.String(80), unique=False, nullable=False)

How would I be able to get all the books that belong to some author?

For example, say for an author, "Richard Dawkins", I’d return "The Blind Watchmaker", "Selfish Gene", "The Ancestor’s Tale".

Any critiques of my models are accepted 🙂

Asked By: lvalue

||

Answers:

To establish a book-author relationship, all that is required is a reference from the author_id column in the books table to the id column in the authors table. This means that the value of the referenced column can be stored in the column defined as ForeignKey. Based on these identical values, an affiliation is defined and a query can be performed.

from sqlalchemy.orm import column_property

class Book(db.Model):
    __tablename__ = 'books'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), unique=False, nullable=False)
    isbn = db.Column(db.String(17), unique=True, nullable=False)
    author_id = db.Column(db.Integer, db.ForeignKey('authors.id'))


class Author(db.Model):
    __tablename__ = 'authors'

    id = db.Column(db.Integer, primary_key=True)
    firstname = db.Column(db.String(80), unique=False, nullable=False)
    lastname = db.Column(db.String(80), unique=False, nullable=False)
    fullname = column_property(firstname + ' ' + lastname)

In order to query all books of an author, all books in which the columns Book.author_id and Author.id are identical are now searched for with the join expression. The author’s full name is filtered, whereby this only corresponds to a mapping of the two columns for the name and is not saved as a separate column.

books = Book.query.join(Author).filter(Author.fullname == 'Richard Dawkins').all()
Answered By: Detlef
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.