4

unique_together ステートメントを含む models.py の sqlall を見ると、強制のように見えるものは何もありません。

私の考えでは、この知識はデータベースが次のようにクエリを最適化するのに役立つ可能性があると想像できます。

「スパム 42 と卵 91 の行は既に見つかっているので、卵 91 の検索で、スパム 42 の行をチェックする必要はもうありません。」

この知識が DB に役立つというのは正しいですか?

このように強制されない (つまり、ORM によってのみ強制される) というのは正しいですか?

両方に「はい」の場合、これは欠陥ですか?

4

1 に答える 1

4

これがどのように見えるべきかの例を次に示します。モデルがあるとします:

class UserConnectionRequest(models.Model):
    sender = models.ForeignKey(UserProfile, related_name='sent_requests')
    recipient = models.ForeignKey(UserProfile, related_name='received_requests')
    connection_type = models.PositiveIntegerField(verbose_name=_(u'Connection type'), \
                                                  choices=UserConnectionType.choices())

    class Meta:
        unique_together = (("sender", "recipient", "connection_type"),)

sqlall を実行すると、以下が返されます。

CREATE TABLE "users_userconnectionrequest" (
    "id" serial NOT NULL PRIMARY KEY,
    "sender_id" integer NOT NULL REFERENCES "users_userprofile" ("id") DEFERRABLE INITIALLY DEFERRED,
    "recipient_id" integer NOT NULL REFERENCES "users_userprofile" ("id") DEFERRABLE INITIALLY DEFERRED,
    "connection_type" integer,
    UNIQUE ("sender_id", "recipient_id", "connection_type")
)

このモデルが DB で適切に同期されている場合、一意の制約 (postgres) があります。

CONSTRAINT users_userconnectionrequest_sender_id_2eec26867fa22bfa_uniq UNIQUE (sender_id、recipient_id、connection_type)、

于 2011-07-26T07:41:22.283 に答える