ActiveRecord::StatementInvalid: Mysql2::Error: Cannot delete or update a parent row - Rails 4.2.6

You can try using this in your group model.

class Group < ActiveRecord::Base
  has_many :chatrooms , dependent: :destroy
end

Now when you execute, Group.last.destroy, it should delete the dependent associated chatrooms before and no hanging data would be left


It seems like you are trying to delete a group that has one or many chatrooms.

But because you added a foreign key constraint (add_foreign_key "chatrooms", "groups"), it is not allowed to delete a group when there is still a chatroom assigned.

To solve this issue you have to destroy all associated chatrooms before destroying the group itself.