0

I am working on a project in Rails 4,previously i was using sqlite3 as a database,i switched to mysql database and changed all its configuration in database.yml file.I also got all my migrations successful while running rake db:migrate.

i was succesfull in ruinning rake db:dump

sudo gem install mysql

rake db:create

the problem i am facing is while running rake db:schema:load

i am getting the following error while running the above command

ActiveRecord::StatementInvalid: Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails: DROP TABLE `advance_salaries` CASCADE
/home/vh/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'
/home/vh/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `block in query'
/home/vh/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `handle_interrupt'
/home/vh/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `query'

dont know why i am facing this issue.please help....................!!!!!!

Mahesh Sharma
  • 221
  • 1
  • 13
  • reply is very urgent for me guys..........so please help..........if anyone knows the solution...... – Mahesh Sharma Mar 26 '17 at 05:10
  • all my project has stopped working... :/ – Mahesh Sharma Mar 26 '17 at 05:10
  • Could you add you schema.rb? Did you try just rake db:migrate instead of schema:load? – denys281 Mar 26 '17 at 10:19
  • yes....rake db:schema:load..........but shows same error...!!! – Mahesh Sharma Mar 26 '17 at 12:34
  • create_table "advance_salaries", force: :cascade do |t| t.integer "employee_id" t.decimal "advance_amount", precision: 15, scale: 2 t.string "no_of_instalment" t.decimal "instalment_amount", precision: 15, scale: 2 t.date "advance_date" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "advance_type_id" t.decimal "interest", precision: 15, scale: 2 end – Mahesh Sharma Mar 26 '17 at 12:36
  • one more child table.....installments..... – Mahesh Sharma Mar 26 '17 at 12:38
  • create_table "instalments", force: :cascade do |t| t.integer "advance_salary_id" t.date "instalment_date" t.decimal "instalment_amount", precision: 15, scale: 2 t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "is_complete", default: false end add_index "instalments", ["advance_salary_id"], name: "index_instalments_on_advance_salary_id" – Mahesh Sharma Mar 26 '17 at 12:38
  • still i am getting same error........... – Mahesh Sharma Mar 26 '17 at 12:38

2 Answers2

0

When you use db:schema:load, rails will clean your db and rebuild schema, and there is exist dependence in your db relation so it bring error.

The detail can reference :

ActiveRecord::StatementInvalid: Mysql2::Error: Cannot delete or update a parent row - Rails 4.2.6

Community
  • 1
  • 1
Hsiu Chuan Tsao
  • 1,396
  • 1
  • 12
  • 23
  • not helping........i was on sqllite database and switched to mysql database and i'm getting this error.Don't know how to solve this issue.......please help..... – Mahesh Sharma Mar 27 '17 at 04:33
  • i am able to migrate,only problem while doing rake db:load. – Mahesh Sharma Mar 27 '17 at 04:34
  • @puneet pls help....if youkniow the solution.....i am in great proble...whole my application has stopped...........!!!!! – Mahesh Sharma Mar 27 '17 at 04:52
  • i have an existing application which is running on sqlite3,so now i want to switch my entire application from sqlite to mysql.I changed all my configurationa nd also was successful in running rake db:migrate.Problem i'm facing while running rake db:schema:load.Dont know my my schema file is not loading.i want to backup all my existing application database from sqlite to mysql. – Mahesh Sharma Mar 27 '17 at 05:03
  • this is my new question link...please help.......... http://stackoverflow.com/questions/43061570/strftime-error-while-switching-my-database-from-sqlite3-to-mysql-in-rails4 – Mahesh Sharma Mar 28 '17 at 05:59
0

Try to follow below command:

rake db:drop
rake db:create
rake db:migrate
rake db:seed
puneet18
  • 4,341
  • 2
  • 21
  • 27
  • thanks guys for all your reply.....i am successful in changing my dtabase from sqlite3 to mysql.But now i am getting strtime error in one of my file.The link for the question is...........http://stackoverflow.com/questions/43061570/strftime-error-while-switching-my-database-from-sqlite3-to-mysql-in-rails4 – Mahesh Sharma Mar 28 '17 at 05:56