0

This is my first publication in stack overflow, I hope you can help me. I'm working with RoR and PostgreSQL, gem 'devise'.


In rails console I am trying to delete data from the "Competitor" table, but I have the following error and I have not been able to solve it.

2.4.1 :006 > c.destroy(c) ActiveRecord::UnknownPrimaryKey: Unknown primary key for table competitors in model Competitor.

This is my competitors table, which it's was generate with model of gem devise

create_table "competitors", id: false, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.bigint "rut"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "lastname"
t.integer "phone"
t.date "dateOfBirth"
t.boolean "gender"
t.string "numberSerie"
t.string "otp_secret_key"
t.integer "otp_module", default: 0
t.index ["email"], name: "index_competitors_on_email", unique: true
t.index ["reset_password_token"], name: "index_competitors_on_reset_password_token", unique: true
t.index ["rut"], name: "index_competitors_on_rut", unique: true

endere

and this is the model

class Competitor < ApplicationRecord


# Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :raffle_registers, primary_key: 'rut', foreign_key: 'rutCompetitors'
  has_many :accountpays
  has_one :found

  #otp model to make it use TFA
  has_one_time_password

  enum otp_module: { disabled: 0, enabled: 1 }, _prefix: true
  attr_accessor :otp_code_token


end
kingninja
  • 35
  • 1
  • 5

1 Answers1

1

You generated the competitors table without primary key. Checkout this line:

create_table "competitors", id: false, force: :cascade do |t|

The id: false is your issue. Check the migration to create the competitors table and set a primary key (or create a new migration adding a primary key to it).

Useful resource: https://stackoverflow.com/a/10079409/740394

Rodrigo
  • 5,435
  • 5
  • 42
  • 78
  • Thank you very much for responding, I am trying to modify the structure of the table through a migration and method "change_table", I delete the id: false, but it keeps maintaining the id: false. I have tried in different ways, you can check if this is ok please. – kingninja Sep 23 '18 at 23:52
  • `class ChangeTableCompetitors < ActiveRecord::Migration[5.1] def change change_table "competitors", force: :cascade do |t| end end end` – kingninja Sep 23 '18 at 23:54