簡體   English   中英

Peewee和數據庫繼承

[英]Peewee and Database Inheritance

我正在嘗試通過做筆記應用來學習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)
    ,...
);

(如果您想在note和notable之間建立M:N關系,則也可以做一個note_notable橋表)。

您需要先在主題,書籍和章節上插入觸發器,然后才能將行插入到noteable中,檢索新行的noteable_id,然后在傳入行中使用該觸發器。

我假設如果您使用的是像Peewee這樣的ORM,則需要在應用程序邏輯中而不是在觸發器中進行。

如何在Peewee中實現此模型?

這是我的方法。 我在Peewee中找不到實現繼承的本地方法,所以我將其滾動為myslef。 如果有更好的方法,請提供您的答案,我會給予獎勵。

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類,並從注解中刪除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;的結果SELECT * FROM NOTABLE;

+----+---------+
| id | type    |
+----+---------+
|  1 | SUBJECT |
|  2 | BOOK    |
|  3 | CHAPTER |
+----+---------+

這是SELECT * FROM NOTE;的結果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 |
+----+-----------------+-------------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM