Answer the question
In order to leave comments, you need to log in
How to correctly organize a reference to a value in a related table?
There are two simple one-to-many tables. Let's say I want to create a field that will be in both the first and second tables (add last_name to the Book table and update it there when updating in Author). Does this make sense or just take the value when querying from the parent table?
class Author(Base):
__tablename__ = 'authors'
id = Column(Integer, primary_key=True)
first_name = Column(String(100), nullable=False)
last_name = Column(String(100), nullable=False)
books = relationship("Book")
class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key=True)
title = Column(String(100), nullable=False)
copyright = Column(SmallInteger, nullable=False)
author_id = Column(Integer, ForeignKey('authors.id'))
Answer the question
In order to leave comments, you need to log in
It makes no sense. It’s not for beauty that you created the author_id property in the Book table in order to link a row from the Author table by it, thereby gaining access to all properties by the author_id identifier.
PS: Are you sure that the book has strictly one author? Or is it just a case study for you?
"I want to wear socks on my head. Does that make sense, or is it better to wear on my feet?"
What is the meaning of your question? If you yourself do not understand "is there any sense" in some action, then maybe not to do it? Well, even without regard to databases, but simply on the basis of minimal common sense?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question