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

PG::InternalError: ERROR: constraint 21715 is not a foreign key constraint #300

Open
brandoncordell opened this issue Aug 30, 2014 · 4 comments

Comments

@brandoncordell
Copy link

I'm getting a fatal error when I try to send messages in my production environment. It works fine in development and on my staging box (mirrors my production environment). I can't seem to get it working no matter what I try.

Here is the error from log/production.log

[2014-08-27T13:38:05.342578 #3327]  INFO -- :   Parameters: {"utf8"=>"✓", "conversation"=>{"recipients"=>"nigora.baeva@mail.ru", "subject"=>"LEGO NINJAGO KAI'S BLADE CYCLE 9441", "body"=>" I have accepted your offer and requesting your shipping address?? thanks again!!"}, "recaptcha_challenge_field"=>"03AHJ_VuvoJbYoRaoId7IIAZo6FCCYe5nCmP8Hdh2N6xzSvlDN7CsuIPOW8pNBAtRISwxGqhrfmbyza_W1_uK5OTfEV3zAK-qT4H26iTJ-pcX0BxOUAnr_vED9Fv1LSAHF1oyS7K1G0SDWuNCgaKSXpv177gSIgKsQk0Exdke6jXzyFovmP-5jK7LdPCgkn4sxBJOt6HIYutPR8LYQqcAaKVn8XsTFmg4ri-0gR1zOBWH06f3j3K09sEC56L1kBtNucudqGSGbY1nmsREIaSBOt630sFCDVpqXN-3TJ0uDaGdDf8O4ScY3vOU", "recaptcha_response_field"=>"1425", "commit"=>"Save Conversation"}
E, [2014-08-27T13:38:05.389887 #3327] ERROR -- : PG::InternalError: ERROR:  constraint 21715 is not a foreign key constraint
: INSERT INTO "mailboxer_notifications" ("body", "conversation_id", "created_at", "sender_id", "sender_type", "subject", "type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"
I, [2014-08-27T13:38:05.390730 #3327]  INFO -- : Completed 500 Internal Server Error in 48ms
F, [2014-08-27T13:38:05.393940 #3327] FATAL -- :
ActiveRecord::StatementInvalid (PG::InternalError: ERROR:  constraint 21715 is not a foreign key constraint
: INSERT INTO "mailboxer_notifications" ("body", "conversation_id", "created_at", "sender_id", "sender_type", "subject", "type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"):
  app/controllers/conversations_controller.rb:16:in `create'```
@w3irdrobot
Copy link
Contributor

After this happens, does the conversation exist in the mailboxer_conversations table?

@brandoncordell
Copy link
Author

No the conversation does not get inserted into the table.

@w3irdrobot
Copy link
Contributor

It's hard to diagnose the problem without seeing more of the code. Can you post your controller method that is calling the send_message method?

@brandoncordell
Copy link
Author

One of my previous developers wrote this, but I think it's pretty standard (based off of a tutorial probably).

class ConversationsController < ApplicationController
  before_filter :require_login

  # load_and_authorize_resource

  helper_method :mailbox, :conversation

  def index
    @conversations ||= current_user.mailbox.inbox.all
  end

  def create
    recipient_emails = conversation_params(:recipients).split(',')
    recipients = User.where(email: recipient_emails).all

    conversation = current_user.send_message(recipients, *conversation_params(:body, :subject)).conversation

    respond_to do |format|
      if conversation.save
        format.html { redirect_to redirect_to conversation_path(conversation), flash: { success: 'Message has sent successfully.' } }
        format.js
      else
        format.html { render :new }
        format.js { render json: { success: false } }
      end
    end
  end

  def reply
    current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
    redirect_to conversation_path(conversation)
  end

  def trashbin
    @trash ||= current_user.mailbox.trash.all
  end

  def trash
    conversation.move_to_trash(current_user)
    redirect_to :conversations
  end

  def untrash
    conversation.untrash(current_user)
    redirect_to :back
  end

  def empty_trash
    current_user.mailbox.trash.each do |conversation|
      conversation.receipts_for(current_user).update_all(deleted: true)
    end

    redirect_to :conversations
  end

  private

  def mailbox
   @mailbox ||= current_user.mailbox
  end

  def conversation
   @conversation ||= mailbox.conversations.find(params[:id])
  end

  def conversation_params(*keys)
   fetch_params(:conversation, *keys)
  end

  def message_params(*keys)
   fetch_params(:message, *keys)
  end

  def fetch_params(key, *subkeys)
   params[key].instance_eval do
     case subkeys.size
     when 0 then self
     when 1 then self[subkeys.first]
     else subkeys.map{|k| self[k] }
     end
   end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants