Please forgive me if this question is ridiculous. I'm relatively new to programming and Django.
Following the typical Publisher, Author, Book example....
Publisher to Author (One to Many)
Author to Book (One to Many)
I'm trying to get Django to give me a list of books published by all the authors under a single publisher.
Two ways I've thought about doing this:
I could get a list of all the authors under a single publisher, and then iterate over these authors to find all of their books. This seems like a really wonky way to do this.. and I'll have to hit the DB a lot.
I can get Django to add a new "Publisher" column in my Books table, but I'm not sure if I want to replicate that data, and I don't know how to get Django to automatically fill in that info.
I'd appreciate any help. Thanks.
If each book has an author ForeignKey and each author has a publisher ForeignKey, you can do:
Book.objects.filter(author__publisher = <Publisher object>)
To get what you're looking for.
Related
There is a Book table that is always unique with title, edition, and author.
And I want all bookstores to add their books, but different bookstores can have the same book with different pricing. So I come up with this table design.
So when one bookstore tries to add a book and the book is already been added by another bookstore the current bookstore should have to just fill in the pricing detail, not including the book detail.
The problem with this is, what if the book detail already been added has some missing or incorrect info? in this case, the current bookstore can flag and moderators or admins can fix it.
Is there any better way to achieve this? I don't comfortable with this design logic at all.
Your design makes sense. You want to keep the "static" information in 1 table, and link "dynamic" information like you did.
Your other question is related to data integrity. You can put "not null" conditions on fields to ensure all fields are filed, but garbage entries are always possible. This is a universal problem.
Potential solutions to mitigate this:
any and all data that can be selected instead of typed in should be linked via another table. Ex:
BookGenre
bookgenreid PK
genre CHAR
Book
bookid PK
genre FK, BookGenre.bookgenreid
...
So you store all possible genres in a separate table, so your users cannot invent new genres or mistype values. Same for authors, countries, ... This makes it easier to build queries as well and avoid things like [ SciFi, Science Fiction, Sciance fiction, ... ]
not everyone should be able to enter new books in the system. Ex. when I worked at a wholesale distributor, only a select group employees could create new products in the database, and they had established a convention on how to do it. They worked closely with purchasing and receiving. You will need to dedicate "data administrators".
So try to control as much as you can in the database and - or the application. Avoid free text fields as much as possible, as users will always think of new ways to mess it up. Ex. at work currently we have a HUGE project to standardise addresses between unlinked systems. It is a enormous undertaking, which involves AI. All this only because no 2 persons enter addresses exactly the same.
Database Design
Is this a bad design for a relational database. I don't see anyone doing examples that look like this.
But considering that an interview is comprised of all the different tables I have linked to the interview table it seems valid.
Except of OH Number (Oral History Number.) An oral history from one narrator may be comprised of different interviews conducted at different dates. Each individual interview is assigned a unique ID that make op a series that is assigned 1 OH Number.
I'm also thinking of putting "Interviewer, Indexer, and Transcriptionist in the same table.
I created the following mock-up for you given the details you have provided. I believe this will be a good starting place. You have an interview object and a person object. You have a joining table of InterviewPerson. This allows you to have one to many person objects per interview.
I want the database to be robust enough that if a researcher called in
and wanted all the interviews conducted by John Doe, on Race
Relations, I could pull a query for it.
To do the aforementioned as you have stated, you would join both the Interview table and the Person table on the InterviewPerson table, and then you would limit your query of that joining based on the Person.firstName, Person.lastName, Interview.topic (or title).
Please note, this is a rough draft but should be a good general idea and start.
Database Design Redux
This is what I came up with based on your suggestions.
I know this might have been asked quite a few times, however I can not find any suitable solution for my problem.
I am implementing database where I have users and articles.
Now the article can be either liked or loved by any of the user.
And here comes the problem, I have to return json that contains list of all articles extended by two fields, liked and loved, because queries are gonna be connected to users.
So liked and loved might be true or false.
I thought about creating two different tables Liked & Loved where I would keep article_id - user_id and if that record exists that means user liked/loved particular article. However I am not quite sure if thats the correct way, nor I have any idea how would I build such query.
If it is important I am using postgresql together with ormlite.
Thanks for any ideas.
If I have understand your question you are basically describing an M-N relationship. A user likes/loves N articles and an article is liked/loved by M users. Such relationships are implemented via a third table that stores the association of users and articles.
You could create a table UserPreferences that links user_id and article_id and has extra columns to indicate if he liked/loved the article.
I can't tell you more about the schema since I don't know if you have other degrees about the preferences (hated, indifferent, confused etc)
suppose I needed to design a database for a bulltin-board website.
something like stackoverflow which means there is a topic and a series of posts
but, no threaded posts (not a tree-based design)
I thought about two main options:
Topic table and Post table. Post has "topic_id" field
no Topic table. only one big Post table.
what do you think is the more preferable option?
Well, stackoverflow is a tagged based design, where a post may have multiple topics/tags.
So to capture this in a relational-style design, you would have three tables:
POST (post_id, author, etc.)
TOPIC (topic_id, name, etc.)
POSTTOPIC (post_id, topic_id)
The reason for POSTTOPIC is because a post may have multiple tags. Using #3, it becomes easy to assign/unassign tags to a post or to find posts with certain topics. None of which a column in POST would be able to accommodate.
I am relatively new to CakePHP, and I am writing a CakePHP application which currently has an Author model and a Book model. Author and Book both have a many-to-many relationship.
However, I would like to additionally, for every author-book relationship, have a corresponding link to that author's blog where they reflect upon their experiences writing that book.
If that was confusing, the following paragraphs try to elaborate to make my situation more clear:
For every author, there will be one article about their experiences for each book. To look at this relationship in reverse, this means that for every book, there will be one corresponding link, per author (given that books can have multiple authors), where that author describes his/her experience writing the book.
In other words, for every single (Author, Book) pair, I would like to be able to store a related URL.
In more fancy, abstracty math terms that I will use horribly and improperly: I have an undirected graph, where every vertex is an Author or a Book, and every edge has an Author for one vertex and a Book for the other. That sounds kind of confusing, but (I think) it describes my situation as precisely as I can make it.
Given the above description, I want to know if I can attach arbitrary data to any given edge of that relation, and retrieve it later.
Currently I have a third model, AuthorBook, which acts the graph edge.
Author has a hasMany relationship to AuthorBook.
Book has a hasMany relationship to AuthorBook.
AuthorBook has a belongsTo relationship with Author and Book, and additionally has a url field.
This solution works, but for some reason the fact that Book is not directly connected to Author is bothering me. Is there any way to achieve this same effect while also cutting the AuthorBook model out of the picture?
(Also, I realize I can have them directly related while keeping AuthorBook, but that seems ugly and also redundant.)
(Also, I realize that I would still have an author_book SQL table. This does not bother me in the slightest, I think it's impossible to do without anyway.)
Thank you for your time! And please don't flame me!!!
Yes, you can model the AuthorBook relation (or the 'edge' as you envision it).
class Author extends AppModel {
...
$hasAndBelongsToMany = array('Book', array('with' => 'AuthorBook'));
....
}
The "with" habtm option:
Defines the name of the model for the join table. By default CakePHP will auto-create a model for you. Using the example above it would be called RecipesTag. By using this key you can override this default name. The join table model can be used just like any "regular" model to access the join table directly.
This is the equivalent of RoR's "through", depicted quite nicely in this illustration