activeadmin-plugins / active_admin_import

:paperclip: active_admin_import is based on activerecord-import gem - the most efficient way to import for ActiveAdmin
http://activeadmin-plugins.github.io/active_admin_import
MIT License
183 stars 101 forks source link

Getting SQL syntax error, as the values in the insert statement is having extra comma at the end. Also 'created_at' value is coming as null. #158

Closed bhargav-prahladarao closed 5 years ago

bhargav-prahladarao commented 6 years ago

---------- Log extract----------------- Started POST "/admin/words/do_import" for 127.0.0.1 at 2018-07-18 06:57:52 +0530 Processing by Admin::WordsController#do_import as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"t2H7W2Raw5VoOcwcTf/1uHyu6Nwan eOhYJZ/6MKlRs7PqTfnAjcG2DZ8XGd/A5BWLrQ5u8gawQGFDzkzAtIFjA==", "active_admin_impo rt_model"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0xb526ca4 @tempfile=#<T empfile:/tmp/RackMultipart20180718-3593-1wovaqo.csv>, @original_filename="dwords _150720182109.csv", @content_type="text/csv", @headers="Content-Disposition: for m-data; name=\"active_admin_import_model[file]\"; filename=\"dwords_150720182109 .csv\"\r\nContent-Type: text/csv\r\n">}, "commit"=>"Import"} AdminUser Load (0.9ms) SELECT admin_users.* FROM admin_users WHERE admi n_users.id = 1 ORDER BY admin_users.id ASC LIMIT 1 (0.7ms) BEGIN (28.4ms) SHOW VARIABLES like 'max_allowedpacket'; Class Create Many Without Validations Or Callbacks (8.4ms) INSERT INTO `word imports(word,meaning,isactive,created_at,updated_at) VALUES ('ನ್ಯೂಹೊ ರೈಜನ್','ನ್ಯೂಹೊರೈಜನ್',1,NULL,'2018-07-18 01:27:52',) ON DUPLICATE KEY UPDATEwor d_imports.updated_at=VALUES(updated_at) Mysql2::Error: You have an error in your SQL syntax; check the manual that corre sponds to your MySQL server version for the right syntax to use near ') ON DUPLI CATE KEY UPDATEword_imports.updated_at=VALUES(updated_at)' at line 1: INS ERT INTOword_imports(word,meaning,isactive,created_at,updated_at) VALUES ('ನ್ಯೂಹೊರೈಜನ್','ನ್ಯೂಹೊರೈಜನ್',1,NULL,'2018-07-18 01:27:52',) ON DUPLICATE KEY UPDATEword_imports.updated_at=VALUES(updated_at) (0.5ms) ROLLBACK Error: Mysql2::Error: You have an error in your SQL syntax; check the manual tha t corresponds to your MySQL server version for the right syntax to use near ') O N DUPLICATE KEY UPDATEword_imports.updated_at=VALUES(updated_at)' at line 1: INSERT INTOword_imports(word,meaning,isactive,created_at,update d_at) VALUES ('ನ್ಯೂಹೊರೈಜನ್','ನ್ಯೂಹೊರೈಜನ್',1,NULL,'2018-07-18 01:27:52',) ON DUP LICATE KEY UPDATEword_imports.updated_at=VALUES(updated_at`)

-------------admin\word.rb------------

before_filter only: [:do_import] do Thread.current['import.word.CreatedBy'] = current_admin_user.id end active_admin_import validate: false, timestamps: true, resource_class: WordImport, template_object: ActiveAdminImport::Model.new( hint: "file will be imported with such header format: 'Word','Meaning','isactive'", csv_headers: ["word","meaning","isactive"], csv_options: { col_sep: ";",row_sep: nil, quote_char: '"' }), before_batch_import: ->(importer) { importer.csv_lines.map{|line| line << Thread.current['import.bus.CreatedBy'] }}, after_import: proc{ Word.transaction do Word.connection.execute("INSERT INTO words (Word, Meaning, CreatedDate, UpdatedDate, IsActive ) (SELECT word, meaning, created_at, updated_at, isactive FROM word_imports)") end }

------------------model---------- WordImport

create_table :word_imports do |t|
    t.string :word, :null => false
    t.string :meaning
    t.boolean :isactive
    t.timestamps null: false
end

Word

create_table :words,:primary_key => :WordID do |t|
    t.string :Word, :null => false
    t.string :Meaning
    t.datetime :CreatedDate, :null =>false
    t.string :CreatedBy, :null => false
    t.datetime :UpdatedDate
    t.string :UpdatedBy
    t.boolean :IsActive
  #t.timestamps null: false
end
workgena commented 6 years ago

Can't figure out why the error popped up.

@bhargav-prahladarao could you please show the "dwords_150720182109.csv"?

bhargav-prahladarao commented 6 years ago

Hi workgena,

I am trying to insert one row with below records:

"ನ್ಯೂಹೊರೈಜನ್";"ನ್ಯೂಹೊರೈಜನ್";"1"

bhargav-prahladarao commented 5 years ago

This issue resolved after changing csv_headers to csv_headers: ["Word","meaning","is_active","created_by"]