A noob question. I am assembling my first database and also have the following design problem: I've got a class which defines a magazine (e.g. it's title) along with a class which defines a webpage (e.g. it's design).
The table for that class book would look so:
Title | PAGE1 | PAGE2 | PAGE3 Book-One | SAMPLE1-UUID | SAMPLE2-UUID | SAMPLE3-UUID Book-Two | SAMPLE4-UUID | SAMPLE5-UUID | SAMPLE6-UUID
The table for that class page:
UUID | FONT | CONTENTS etc. SAMPLE1-UUID | Times | Example SAMPLE2-UUID | Arial | Example Two SAMPLE3-UUID | Verdena | Example Three
Now, as each page is exclusive and should not be re-utilized in another book, I can not make use of a many-to-many relationship for Pages. I possibly could use Foreign-Answer to link the 2 tables, i.e. link SAMPLE1-UUID from the Books Table using the SAMPLE1-UUID from the Pages Table. It has the benefit of not creating exactly the same entry two times.
However, I do not like the thought of getting a set quantity of rows for my pages. Within the above example for that class Book, I'd need to define a particular group of Pages, like PAGE1, PAGE2, PAGE3, PAGE4, ... PAGE99. Ideally, all I want is really a flexible listing of pages for my book class, like so:
Name | Pages Book-One | "SAMPLE1-UUID, SAMPLE2-UUID" Book-Two | "SAMPLE4-UUID, SAMPLE5-UUID, SAMPLE6-UUID"
Pages will be a simple CharField and it is contents could be a listing. However I've the issue the two tables aren't linked any longer which I'd need to create each entry two times (i.e. I would need to enter SAMPLE1-UUID both in the web pages and books table).
Can there be a different way to design this database? Thank you for any suggestion!
I'll suggest you do not have the web pages as posts:
The table for that class book would look so with book only information:
Title | ISBN Book-One | XXXXXXXXXXXX Book-Two | YYYYYYYYYYYY
The table for that class page:
BOOKID |PAGE_NUM | FONT | CONTENTS 1 |1 | Times | Example 1 |2 | Arial | Example Two 2 |1 | Verdena | Example Three
Your class design would look something similar to:
class Book(models.Model): title = models.CharField(max_length=100) isbn = models.CharField(max_length=100) class Page(models.Model): book = models.ForeignKey(Book) page_num = models.IntegerField() font = models.charField(max_length=100) content = models.TextField()
You are able to proceed and also have contraints to ensure that a magazine and page_num doesn't repeat for example but this is often a nice beginning.
I'd get it done such as this:
class Book(models.Model): name=models.CharField(max_length=....) class Page(models.Model): book=models.ForeignKey(Book) number=models.PositiveIntegerField()
I do not understand your book table example: Would you like a column for page1 and some other column for page2? That appears very strange.
You've misinterpreted the way a foreign key works. It isn't a "fixed quantity of rows" - quite contrary, actually.
As guettli shows in the answer, a ForeignKey area is really a one-to-many relationship, defined around the "many" side. That's, using the ForeignKey defined on-page pointing at Book, each Page has one Book, but a magazine has as numerous Pages as you desire.
So, while using Django ORM, for those who have a magazine object and you need to get its pages, you simply do