Rails: why cannot delete dependent table if dependent: :destroy allowed?
<p>The article_id is in the Pictures table, as it has to be, because each article can have many pictures.</p>

<p>so the Picture table would look like this:</p>

<pre><code>id        article_id         picture

1           34              pinguin.jpg
2           56              koala.jpg
3           56              bear.jpg
</code></pre>

<p>etc </p>

<p>I know the difference between destroy_all (which instantiates an object on the dependent table and calls its destroy method and it is a slow process) and delete_all which simply wipes out records without caring about associations.</p>

<p>But I have to use destroy_all because THERE ARE associated dependent records:</p>

<pre><code>Article Model
    class Article &lt; ActiveRecord::Base
      belongs_to :user
      has_many :pictures , **`dependent: :destroy`**
      accepts_nested_attributes_for :pictures

Picture Model
    class Picture &lt; ActiveRecord::Base
      belongs_to :article
     mount_uploader :picture, PictureUploader

     def destroy
     end
</code></pre>

<p>SCHEMA</p>

<pre><code> create_table "articles", force: :cascade do |t|
    t.string   "country"
    t.string   "region"
    t.string   "town"
    t.string   "street"
    t.string   "company"
    t.string   "title"
    t.text     "content"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "articles", ["user_id", "created_at"], name: "index_articles_on_user_id_and_created_at", using: :btree
  add_index "articles", ["user_id"], name: "index_articles_on_user_id", using: :btree

  create_table "pictures", force: :cascade do |t|
    t.integer  "article_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "picture"
  end
</code></pre>

<p>So, both commands, Article.delete_all and Article.destroy_all</p>

<p>bring a similar complaint</p>

<pre><code>Article.destroy_all
  Article Load (0.9ms)  SELECT "articles".* FROM "articles"  ORDER BY "articles"."created_at" DESC
   (0.1ms)  BEGIN
  SQL (1.7ms)  DELETE FROM "articles" WHERE "articles"."id" = $1  [["id", 21]]
PG::ForeignKeyViolation: ERROR:  update or delete on table "articles" violates foreign key constraint "fk_rails_658164416e" on table "pictures"
DETAIL:  Key (id)=(21) is still referenced from table "pictures".
: DELETE FROM "articles" WHERE "articles"."id" = $1
   (0.2ms)  ROLLBACK


class CreatePictures &lt; ActiveRecord::Migration
  def change
    create_table :pictures do |t|
      t.references :article, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end
</code></pre>

<p>UPDATE</p>

<p>I have tried adding the on delete cascade FK in the migration files, to no avail:</p>

<p>I first removed the FK as I had to add the qualifier: on_delete: :cascade</p>

<pre><code>class RemoveFkFromPictures &lt; ActiveRecord::Migration
  def change

    remove_foreign_key :pictures, column: :article_id
  end
end
</code></pre>

<p>I created another migration file to add the FK</p>

<pre><code>class AddForeignKeyToPictures1 &lt; ActiveRecord::Migration
  def change
    add_foreign_key :pictures, :articles, on_delete: :cascade , name: :deletingarticlescascadesonpictures

  end
end
</code></pre>

<p>and the schema looks like:</p>

<pre><code>add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree

  add_foreign_key "articles", "users"
  add_foreign_key "pictures", "articles", name: "deletingarticlescascadesonpictures", on_delete: :cascade
end
</code></pre>

<p>so, you would think that it is ok now. Well, it is not:</p>

<pre><code>  Article Load (1.3ms)  SELECT "articles".* FROM "articles"  ORDER BY "articles"."created_at" DESC
   (0.2ms)  BEGIN
  Picture Load (0.5ms)  SELECT "pictures".* FROM "pictures" WHERE "pictures"."article_id" = $1  [["article_id", 25]]
   (0.4ms)  ROLLBACK
ActiveRecord::RecordNotDestroyed: Failed to destroy the record
</code></pre>