fnando / coupons

Coupons is a Rails engine for creating discount coupons.
MIT License
141 stars 113 forks source link

PG::NotNullViolation: ERROR: null value in column "attachments" violates not-null constraint #14

Open kranthi1027 opened 8 years ago

kranthi1027 commented 8 years ago

Hi @fnando , Great work on the gem. I was looking for exactly something like this and this surely is of great use.

So, I was trying to include this gem in my application and came across above titled error. I followed all the instructions in my local machine where I'm using MySQL and it works like a charm.

I have uploaded the code to heroku (Postgre Sql) just by pushing the master and it throws me an Internal Server Error.

Here is the trace of the error:

 Started GET "/coupons" for 183.83.215.45 at 2015-12-10 19:26:45 +0000
2015-12-10T19:26:45.073575+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/coupons/_no_coupons.html.erb (1.5ms)
2015-12-10T19:26:45.073658+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/coupons/index.html.erb within layouts/coupons/application (45.7ms)
2015-12-10T19:26:45.080975+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/application/_header.html.erb (0.9ms)
2015-12-10T19:26:45.086024+00:00 app[web.1]: Completed 200 OK in 79ms (Views: 60.1ms | ActiveRecord: 6.9ms)
2015-12-10T19:26:45.007191+00:00 app[web.1]: Processing by Coupons::CouponsController#index as HTML
2015-12-10T19:26:45.084923+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/application/_flash_messages.html.erb (0.8ms)
2015-12-10T19:26:45.094477+00:00 heroku[router]: at=info method=GET path="/coupons" host=migreratest.herokuapp.com request_id=9dc1a74f-128c-4251-94f5-83b1d07aefe0 fwd="183.83.215.45" dyno=web.1 connect=1ms service=88ms status=304 bytes=555
2015-12-10T19:26:47.722715+00:00 app[web.1]: Started GET "/coupons/new" for 183.83.215.45 at 2015-12-10 19:26:47 +0000
2015-12-10T19:26:47.730271+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/application/_form_errors.html.erb (0.1ms)
2015-12-10T19:26:47.749549+00:00 heroku[router]: at=info method=GET path="/coupons/new" host=migreratest.herokuapp.com request_id=3a45c6e6-7fdf-4ebf-b100-1bfaf4631866 fwd="183.83.215.45" dyno=web.1 connect=1ms service=26ms status=200 bytes=8323
2015-12-10T19:26:47.738517+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/coupons/_form.html.erb (8.9ms)
2015-12-10T19:26:47.725380+00:00 app[web.1]: Processing by Coupons::CouponsController#new as HTML
2015-12-10T19:26:47.738580+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/coupons/new.html.erb within layouts/coupons/application (9.9ms)
2015-12-10T19:26:47.740022+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/application/_header.html.erb (0.1ms)
2015-12-10T19:26:47.740197+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/bundler/gems/coupons-0bed9cfad48a/app/views/coupons/application/_flash_messages.html.erb (0.1ms)
2015-12-10T19:26:47.740651+00:00 app[web.1]: Completed 200 OK in 15ms (Views: 12.7ms | ActiveRecord: 0.0ms)
2015-12-10T19:27:18.995574+00:00 app[web.1]: Processing by Coupons::CouponsController#create as HTML
2015-12-10T19:27:18.995618+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "coupon"=>{"code"=>"MIGRERA100", "description"=>"Checking", "redemption_limit"=>"1", "type"=>"amount", "amount"=>"100", "valid_from(1i)"=>"2015", "valid_from(2i)"=>"12", "valid_from(3i)"=>"11", "valid_until(1i)"=>"2016", "valid_until(2i)"=>"1", "valid_until(3i)"=>"10"}, "commit"=>"Create Coupon"}
2015-12-10T19:27:19.013028+00:00 app[web.1]: PG::NotNullViolation: ERROR:  null value in column "attachments" violates not-null constraint
2015-12-10T19:27:19.013035+00:00 app[web.1]: DETAIL:  Failing row contains (2, MIGRERA100, Checking, 2015-12-11, 2016-01-10, 1, 0, 100, amount, 2015-12-10 19:27:19.003908, 2015-12-10 19:27:19.003908, null).
2015-12-10T19:27:19.013038+00:00 app[web.1]: : INSERT INTO "coupons" ("attachments", "code", "description", "amount", "type", "valid_from", "valid_until", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
2015-12-10T19:27:19.019329+00:00 app[web.1]: Completed 500 Internal Server Error in 24ms
2015-12-10T19:27:18.987570+00:00 app[web.1]: Started POST "/coupons" for 183.83.215.45 at 2015-12-10 19:27:18 +0000
2015-12-10T19:27:19.023813+00:00 app[web.1]: DETAIL:  Failing row contains (2, MIGRERA100, Checking, 2015-12-11, 2016-01-10, 1, 0, 100, amount, 2015-12-10 19:27:19.003908, 2015-12-10 19:27:19.003908, null).
2015-12-10T19:27:19.023810+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR:  null value in column "attachments" violates not-null constraint
2015-12-10T19:27:19.023816+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:602:in `exec_prepared'
2015-12-10T19:27:19.023817+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
2015-12-10T19:27:19.023818+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
2015-12-10T19:27:19.023814+00:00 app[web.1]: : INSERT INTO "coupons" ("attachments", "code", "description", "amount", "type", "valid_from", "valid_until", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"):
2015-12-10T19:27:19.023819+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
2015-12-10T19:27:19.023817+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:602:in `block in exec_cache'
2015-12-10T19:27:19.023805+00:00 app[web.1]: 
marcelokanzaki commented 8 years ago

ALTER TABLE coupons ALTER COLUMN attachments DROP NOT NULL;

kranthi1027 commented 8 years ago

That helped. Thanks.

dedman commented 8 years ago

If you want to use the migration api, then use this.

change_column :coupons, :attachments, :text, default: '{}', null: true