ruby on rails - Unique multiple key index issue in production (Heroku PostgreSQL) -
i facing issue have tried solve myself don't what's wrong. french models, controllers... have french names.
i developing app association makes call in french "maraudes" every night : driving around meet people.
i have maraude model :
class maraude < activerecord::base default_scope -> { order(date: :desc) } validates :date, presence: true, uniqueness: { scope: :type_maraude } validates :type_maraude, presence: true end
my maraudes have :date
, :type_maraude
attributes, , want them define every maraude, every maraude has unique [:date, :type_maraude]
combination.
in order this, created migration :
class addindextomaraudes < activerecord::migration def change add_index :maraudes, [:date, :type_maraude], unique: true end end
it working in development. in production (heroku), have created maraude dated 2016-02-02 type (maraude salariés 1) , when trying create other maraude @ same date different type following error , don't understand may come :
2016-02-05t13:56:51.810031+00:00 app[web.1]: started post "/maraudes" 80.13.244.250 @ 2016-02-05 13:56:51 +0000 2016-02-05t13:56:51.823195+00:00 app[web.1]: maraude exists (0.9ms) select 1 one "maraudes" ("maraudes"."date" = '2016-02-02' , "maraudes"."type_maraude" = 'maraude bénévoles') limit 1 2016-02-05t13:56:51.812447+00:00 app[web.1]: processing maraudescontroller#create html 2016-02-05t13:56:51.829572+00:00 app[web.1]: completed 500 internal server error in 17ms (activerecord: 5.9ms) 2016-02-05t13:56:51.819326+00:00 app[web.1]: maraude load (0.9ms) select "maraudes".* "maraudes" "maraudes"."date" = $1 , "maraudes"."type_maraude" = $2 order "maraudes"."date" desc limit 1 [["date", "2016-02-02"], ["type_maraude", "maraude bénévoles"]] 2016-02-05t13:56:51.812533+00:00 app[web.1]: parameters: {"utf8"=>"✓", "authenticity_token"=>"useo/o+npj6cemrrfdun2lrisdcahiseytlv2ekxigh/6c47zzehtm0mcra7euy/qy/nzuxdpfntdxr4vugudw==", "maraude"=>{"date"=>"2016-02-02", "type_maraude"=>"maraude bénévoles"}, "commit"=>"créer la maraude"} 2016-02-05t13:56:51.828110+00:00 app[web.1]: detail: key (date)=(2016-02-02) exists. 2016-02-05t13:56:51.814745+00:00 app[web.1]: user load (0.9ms) select "users".* "users" "users"."id" = $1 limit 1 [["id", 1]] 2016-02-05t13:56:51.820512+00:00 app[web.1]: (0.7ms) begin 2016-02-05t13:56:51.828073+00:00 app[web.1]: sql (1.6ms) insert "maraudes" ("date", "type_maraude", "villes", "created_at", "updated_at") values ($1, $2, $3, $4, $5) returning "id" [["date", "2016-02-02"], ["type_maraude", "maraude bénévoles"], ["villes", ""], ["created_at", "2016-02-05 13:56:51.823377"], ["updated_at", "2016-02-05 13:56:51.823377"]] 2016-02-05t13:56:51.828108+00:00 app[web.1]: pg::uniqueviolation: error: duplicate key value violates unique constraint "index_maraudes_on_date" 2016-02-05t13:56:51.828111+00:00 app[web.1]: : insert "maraudes" ("date", "type_maraude", "villes", "created_at", "updated_at") values ($1, $2, $3, $4, $5) returning "id" 2016-02-05t13:56:51.829215+00:00 app[web.1]: (0.9ms) rollback 2016-02-05t13:56:51.832064+00:00 app[web.1]: 2016-02-05t13:56:51.832067+00:00 app[web.1]: activerecord::recordnotunique (pg::uniqueviolation: error: duplicate key value violates unique constraint "index_maraudes_on_date" 2016-02-05t13:56:51.832068+00:00 app[web.1]: detail: key (date)=(2016-02-02) exists. 2016-02-05t13:56:51.832069+00:00 app[web.1]: : insert "maraudes" ("date", "type_maraude", "villes", "created_at", "updated_at") values ($1, $2, $3, $4, $5) returning "id"): 2016-02-05t13:56:51.832070+00:00 app[web.1]: app/controllers/maraudes_controller.rb:34:in `create'
the pg error seems indicate behaves had created unique index on :date
not case...
for info here 'create' action in maraudes controller. edit : translated flash messages.
def create @maraude = maraude.new(maraude_params) @maraude.villes = "" if maraude.find_by(date: params[:maraude][:date], type_maraude: params[:maraude][:type_maraude]) flash[:danger] = "this maraude exists" redirect_to new_maraude_path elsif @maraude.save flash[:success] = "maraude created" redirect_to id_m_villes_path(id: @maraude.id) else flash[:danger] = "give date , maraude type" redirect_to new_maraude_path end end
thx !
Comments
Post a Comment