teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2019/11/12 14:52

投稿

shumbow
shumbow

スコア35

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,20 @@
1
- rails console -sandbox で user = User.first user.delete していようとしているのですが紐づいているモデルがActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_3d31dad1cc" on table "articles")で削除できません。
1
+ rails console -sandbox で user = User.first user.delete していようとしているのですが紐づいているモデルがActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_3d31dad1cc" on table "articles")DETAIL: Key (id)=(86a4d73c-071b-405d-9a6c-2787ac1f02a2) is still referenced from table "articles".
2
+ で削除できません。
2
3
  userモデルには has_many :articles, dependent: :nullify とdependentオプションも指定しています。
3
4
  schema.rbに add_foreign_key "articles", "users" と書いてあるのですが
4
5
  そもそもdependentオプションをつけるときは外部キー制約をつけないのでしょうか?
5
6
  またrails console だとうまくいかないとかあるのでしょうか?
6
7
 
8
+ 追記
9
+ ちなみにユーザーは他にも子を持っています
10
+
11
+ ** user.rb**
12
+ has_many :articles, dependent: :nullify
13
+ has_many :items, dependent: :nullify
14
+ has_many :votes, dependent: :nullify
15
+
16
+
17
+ 入れ子になっていてarticle はitemを持っていて、item はvoteを持っています。
18
+ エラーはarticleとuserについてなので関係ない気もしますが、、。
19
+
7
20
  rails6 ruby 2.6.3