ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR: ....) Can not rescue?

144 views
Skip to first unread message

smar...@gmail.com

unread,
Oct 13, 2016, 6:52:00 PM10/13/16
to ruby-pg
ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR: null value in column "xxxx" violates not-null constraint DETAIL: .....)

I have code to rescue exception, but it does not work!

def create
 
begin
 
@model =Model.create!(params)
 
#if params do not contain the not null column
  render json
: @model, status: 201
 
rescue
  render status
: 403
 
end
end

The server has a response with 200 OK....Neither 201 nor 403....

My question :

If I have a not null column in database and have not set 'not null' in ActiveRecord, How can I catch this ERROR.

Lars Kanis

unread,
Oct 14, 2016, 12:44:29 AM10/14/16
to rub...@googlegroups.com

You should use validations in this case or at least rescue a more specific exception class.

However this is not the right mailing list for these kind of issues. Please ask on a rails list.

--
Regards Lars

John Smart

unread,
Nov 2, 2016, 10:35:50 AM11/2/16
to ruby-pg
Check if the rescued error is_a? PG::NotNullViolation or rescue that type specifically.
Reply all
Reply to author
Forward
0 new messages