Rails / MySQL ActiveRecord :: RecordNotUnique

时间:2017-07-12 10:07:22

标签: mysql ruby-on-rails ruby-on-rails-4 activerecord

我有这个令人困惑的错误消息ActiveRecord::RecordNotUnique

这是我的模型:artisan.rb

class Artisan < ActiveRecord::Base
  require 'csv'

  validates :nom_entreprise, presence: true
  validates :nom_entreprise_format_url, presence: true
  validates :adresse_1, presence: true
  validates :code_postal, presence: true
  validates :ville, presence: true
  validates :gps_latitude, presence: true
  validates :gps_longitude, presence: true
  validates :url_site_artisan, presence: true
  validates :telephone, presence: true

  csv_text = File.read('artisans.csv')
  csv = CSV.parse(csv_text, :headers => true)
  csv.each do |row|
    Artisan.create!(row.to_hash)
  end
end

控制器:

class ArtisansController < ApplicationController
  def index
    @artisans = Artisan.all 
  end
end

我也给你schema.rb

  create_table "artisans", force: :cascade do |t|
    t.string   "nom_entreprise",            limit: 255
    t.string   "nom",                       limit: 255
    t.string   "prenom",                    limit: 255
    t.string   "adresse_1",                 limit: 255
    t.string   "adresse_2",                 limit: 255
    t.integer  "code_postal",               limit: 4
    t.string   "ville",                     limit: 255
    t.float    "gps_latitude",              limit: 24
    t.float    "gps_longitude",             limit: 24
    t.string   "description",               limit: 255
    t.string   "url_site_artisan",          limit: 255
    t.string   "url_voir_plus",             limit: 255
    t.float    "telephone",                 limit: 24
    t.string   "nom_entreprise_format_url", limit: 255
    t.boolean  "visible_site_web"
    t.datetime "created_at",                            null: false
    t.datetime "updated_at",                            null: false
  end

最后但并非最不重要的是,错误消息:

Started GET "/admin/artisans" for ::1 at 2017-07-12 11:40:37 +0200
Processing by RailsAdmin::MainController#index as HTML
  Parameters: {"model_name"=>"artisans"}
[RailsAdmin] Could not load model Artisans, assuming model is non existing. (uninitialized constant Artisans)
   (0.2ms)  BEGIN
  SQL (1.0ms)  INSERT INTO `artisans` (`id`, `nom_entreprise`, `nom`, `prenom`, `adresse_1`, `adresse_2`, `code_postal`, `ville`, `gps_latitude`, `gps_longitude`, `description`, `url_site_artisan`, `url_voir_plus`, `telephone`, `nom_entreprise_format_url`, `visible_site_web`, `created_at`, `updated_at`) VALUES (10, 'La Brasserie du Baril', 'NULL', 'NULL', '4, Rue Champlain', 'NULL', 29200, 'Brest', 48.3818645, -4.529462500000022, 'NULL', 'http://www.brasseriedubaril.com', 'NULL', 9.0, 'la-brasserie-du-baril', 1, '2017-07-12 09:40:37', '2017-07-12 09:40:37')
   (0.2ms)  ROLLBACK
Completed 500 Internal Server Error in 24ms (ActiveRecord: 1.4ms)

ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '10' for key 'PRIMARY': INSERT INTO `artisans` (`id`, `nom_entreprise`, `nom`, `prenom`, `adresse_1`, `adresse_2`, `code_postal`, `ville`, `gps_latitude`, `gps_longitude`, `description`, `url_site_artisan`, `url_voir_plus`, `telephone`, `nom_entreprise_format_url`, `visible_site_web`, `created_at`, `updated_at`) VALUES (10, 'La Brasserie du Baril', 'NULL', 'NULL', '4, Rue Champlain', 'NULL', 29200, 'Brest', 48.3818645, -4.529462500000022, 'NULL', 'http://www.brasseriedubaril.com', 'NULL', 9.0, 'la-brasserie-du-baril', 1, '2017-07-12 09:40:37', '2017-07-12 09:40:37')):
  app/models/artisan.rb:17:in `block in <class:Artisan>'
  app/models/artisan.rb:16:in `<class:Artisan>'
  app/models/artisan.rb:1:in `<top (required)>'

开头的错误消息表示无法加载模型“artisans”,因为它不存在。这是真的!为了创建这个模型,我运行rails g model Artisan rails g model Artisans

最后一件事:错误发生在管理界面中,但也发生在视图中(视图中没有任何内容)。 我能够在管理员中看到Artisan的内容然后离开并显示错误消息。

任何形式的帮助都将不胜感激! :)

1 个答案:

答案 0 :(得分:0)

在SQL中,PRIMARY KEY约束唯一标识数据库表中的每条记录。主键必须包含UNIQUE值,并且不能包含NULL值。而且,一个表只能有一个主键。 默认情况下,Rails负责维护数据库中唯一的主键(即id)。它还验证了主键的唯一性。但是,上面的代码正在尝试保存重复的ID&#39;使用代码Artisan.create!(row.to_hash)

您可以通过省略&#34; id&#34;来解决此问题。创建过程中的属性。

 csv.each do |row|
   Artisan.create!(row.to_hash.except("id"))
 end