Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance/#48 aad foreign key to comments #64

Merged
merged 6 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
# content(内容) :text(65535) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :integer not null
# memo_id(メモID) :bigint not null
#
# Indexes
#
# index_comments_on_memo_id (memo_id)
#
# Foreign Keys
#
# fk_rails_... (memo_id => memos.id)
#
class Comment < ApplicationRecord
validates :content, presence: true
belongs_to :memo
Expand Down
4 changes: 3 additions & 1 deletion backend/db/Schemafile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ create_table 'memos', charset: 'utf8mb4', collation: 'utf8mb4_0900_ai_ci', force
end

create_table 'comments', charset: 'utf8mb4', collation: 'utf8mb4_0900_ai_ci', force: :cascade do |t|
t.integer "memo_id", null: false, comment: 'メモID'
t.bigint "memo_id", null: false, comment: 'メモID'
t.text "content", null: false, comment: '内容'
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["memo_id"], name: "index_comments_on_memo_id"
end

add_foreign_key 'comments', 'memos'
Copy link
Collaborator

@kuri0616 kuri0616 Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

外部キーですが明示的に名前をつける方が良いと思いました!
理由は以下の通りです!

  • 外部キーが何を指しているのか明確になる
  • エラーなどで問題が発生した際の発見に容易になるケースがある(ログなどに外部キー名が出力されることもある)
    です!
    今の状態だと以下のような自動生成された名前になっています!
CONSTRAINT `fk_rails_0f1031a78b` FOREIGN KEY (`memo_id`) REFERENCES `memos` (`id`) `

これが外部キー名になります。
fk_rails_0f1031a78b

調べた感じだと
fk_<参照元テーブル>_<参照先テーブル>_<参照するカラム>
とするパターンが多いようですね!
nameオプションをつけることで命名ができるのでそのようにお願いします🙇‍♂️

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます🙇‍♂️
Ridgepole初めて使ったので、外部キーの命名方法など、初めて知りました。勉強になります📝
外部キー名は、fk_comments_memo_idにしようと思います🫡

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMです!

3 changes: 2 additions & 1 deletion backend/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion backend/spec/factories/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
# content(内容) :text(65535) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :integer not null
# memo_id(メモID) :bigint not null
#
# Indexes
#
# index_comments_on_memo_id (memo_id)
#
# Foreign Keys
#
# fk_rails_... (memo_id => memos.id)
#

FactoryBot.define do
factory :comment do
Expand Down
6 changes: 5 additions & 1 deletion backend/spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
# content(内容) :text(65535) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :integer not null
# memo_id(メモID) :bigint not null
#
# Indexes
#
# index_comments_on_memo_id (memo_id)
#
# Foreign Keys
#
# fk_rails_... (memo_id => memos.id)
#
RSpec.describe Comment do
subject(:comment) { build(:comment) }

Expand Down
Loading