Peewee和数据库继承

4
我将尝试通过制作一款笔记应用程序来学习Peewee和Bottle技术。
假设我有以下实体:
Subject
Book
Chapter
Note
Tag

我希望您能够为章节、书籍和主题制作笔记,并使其易于理解。

在数据库中,您可以这样做:

create table noteable (
    noteable_id INT AUTO_INCREMENT PRIMARY KEY
    ,type VARCHAR(10) NOT NULL CHECK (type in ('SUBJECT','BOOK','CHAPTER','NOTE'))
);
create table subject (
    subject_id INT AUTO_INCREMENT PRIMARY KEY
    ,noteable_id INT UNIQUE REFERENCES noteable (noteable_id)
    ,...
);
create table book (
    book_id INT AUTO_INCREMENT PRIMARY KEY
    ,subject_id INT NOT NULL REFERENCES subject (subject_id)
    ,noteable_id INT UNIQUE REFERENCES noteable (noteable_id)
    ,...
);
create table chapter(
    chapter_id INT AUTO_INCREMENT PRIMARY KEY
    ,book_id INT NOT NULL REFERENCES book (book_id)
    ,noteable_id INT UNIQUE REFERENCES noteable(noteable_id)
    ,...
);

create table note(
    note_id INT AUTO_INCREMENT PRIMARY KEY
    ,noteable_id INT UNIQUE REFERENCES noteable(noteable_id)
    ,...
);

如果您想要在笔记和相关的实体之间建立M:N的关系,您需要创建一个note_notable桥接表。
您需要在subject、book和chapter上设置before insert触发器,在noteable中插入一行记录,检索新行的noteable_id,并将其用于即将到来的行。
假设您正在使用像Peewee这样的ORM,您会希望在应用程序逻辑中完成这个过程而不是使用触发器。
如何在Peewee中实现这种模型?
1个回答

6

以下是我的操作步骤。在Peewee中,我无法找到本地实现继承的方法,所以我自己实现了它。如果您知道更好的方法,请提供您的答案,我会给予奖励。

import MySQLdb
import peewee
from peewee import *
from datetime import datetime

db = MySQLDatabase('test', user='root',passwd='psswd')

class BaseModel(Model):
    class Meta:
        database = db

class Noteable(BaseModel):
    type = CharField(null = False)

# This will act as the trigger that inserts a row into noteable,
# and retrieves the notable.id to use
class N(BaseModel):
    def save(self, *args, **kwargs):
        if not self.id:
            noteable = Noteable(type=self.__class__.__name__.upper())
            noteable.save()
            self.noteable = noteable.id
        return super(N, self).save(*args, **kwargs)

class Subject(N):
    name = CharField(null = False, unique = True)
    noteable = ForeignKeyField(Noteable, related_name="noteablesubject", null= False, unique = True)


class Book(N):
    name = CharField(null = False, unique = True)
    subject = ForeignKeyField(Subject, related_name="books", null = False)
    noteable = ForeignKeyField(Noteable, related_name="noteablebook", null= False, unique = True)


class Chapter(N):
    name = CharField(null = False)
    chapter_number = IntegerField(null = False)
    book = ForeignKeyField(Book, related_name="chapters")
    noteable = ForeignKeyField(Noteable, related_name="noteablechapter", null= False, unique = True)


class Note(BaseModel):
    note = TextField(null = False)
    # N.B. unique is not true, as multiple notes can go to the same subject/book/chapter
    noteable = ForeignKeyField(Noteable, related_name="notes", null= False)


Note.drop_table(True)
Chapter.drop_table(True)
Book.drop_table(True)
Subject.drop_table(True)
Noteable.drop_table(True)

Noteable.create_table(True)
Subject.create_table(True)
Book.create_table(True)
Chapter.create_table(True)
Note.create_table(True)

s = Subject(name="subject")
s.save()
n = Note(note="subject notes", noteable = s.noteable)
n.save()
n = Note(note="subject notes 2", noteable = s.noteable)
n.save()
b = Book(name="book", subject=s)
b.save()
n = Note(note="book notes", noteable = b.noteable)
n.save()
n = Note(note="book notes 2", noteable = b.noteable)
n.save()
c = Chapter(chapter_number=1, name="chapter", book=b)
c.save()
n = Note(note="chapter notes", noteable=c.noteable)
n.save()
n = Note(note="chapter notes 2", noteable=c.noteable)
n.save()

如果您希望笔记和值得注意的事物之间建立多对多关系,则需要定义一个NoteNotable类,该类具有外键,并从Note中删除FK。
您可以定义一个辅助方法,以左连接任何类与笔记:
def get_notes(clazz, id):
    return clazz.select().join(Noteable).join(Note, JOIN_LEFT_OUTER).where(clazz.id = id)

您可以像这样迭代它:
% for note in chapter.noteable.notes:

% end

这里是一个 SELECT * FROM NOTABLE; 的查询结果。
+----+---------+
| id | type    |
+----+---------+
|  1 | SUBJECT |
|  2 | BOOK    |
|  3 | CHAPTER |
+----+---------+

这里是从SELECT * FROM NOTE;查询得到的结果。
+----+-----------------+-------------+
| id | note            | noteable_id |
+----+-----------------+-------------+
|  1 | subject notes   |           1 |
|  2 | subject notes 2 |           1 |
|  3 | book notes      |           2 |
|  4 | book notes 2    |           2 |
|  5 | chapter notes   |           3 |
|  6 | chapter notes 2 |           3 |
+----+-----------------+-------------+

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接