你好,
我想报告一下模型中存在但数据库中缺失的一些验证/约束问题。
缺失的非空约束
PostDetail - key, value 在模型中是必填项,但在数据库中允许为空。
-
模型中要求(
app/models/post_detail.rb):validates_presence_of :key, :value…但在数据库中允许为空
discourse_development=# \d post_details Table "public.post_details" Column | Type | Collation | Nullable | Default ------------+-----------------------------+-----------+----------+------------------------------------------ ... key | character varying | | | value | character varying | | |如果我们在数据库中添加 NOT NULL 约束,空字符串仍有可能通过,但我认为添加非空约束至少会稍微安全一些。
缺失的唯一约束
-
TagGroup - name。模型中要求唯一(
app/models/tag_group.rb)validates_uniqueness_of :name, case_sensitive: false但数据库中缺少唯一索引
discourse_development=# \d tag_groups ... Indexes: "tag_groups_pkey" PRIMARY KEY, btree (id) -
同样,WatchedWord - word。(
app/models/watched_word.rb)validates :word, presence: true, uniqueness: true, length: { maximum: 50 }数据库中对
(action, word)有唯一索引,但对单独的(word)没有:discourse_development=# \d watched_words ... Indexes: "watched_words_pkey" PRIMARY KEY, btree (id) "index_watched_words_on_action_and_word" UNIQUE, btree (action, word) -
最后,WebHookEventType - name。(
app/models/web_hook_event_type.rb):validates :name, presence: true, uniqueness: true数据库中缺少唯一约束:
discourse_development=# \d web_hook_event_types ... Indexes: "web_hook_event_types_pkey" PRIMARY KEY, btree (id)
或许有人能确认这些是否合理。它们可能有助于在问题发生之前预防错误。此外,如果是这样,修复方案似乎相当直接,如果需要的话,我很乐意创建拉取请求。
谢谢。