简体   繁体   English

Rake:db迁移失败,并带有预先存在的mySQL数据库

[英]Rake:db migrate failing with pre-existing mySQL database

I am trying to create a rails instance with a pre-existing mySQL database and I am following this guide . 我正在尝试使用预先存在的mySQL数据库创建Rails实例,并且正在遵循本指南

Steps 1-3 worked but when running the rails server, it says there are migrations pending. 第1-3步有效,但是在运行Rails服务器时,它说有迁移挂起。

I would be very grateful if someone could tell me where the error is stemming from or give some general pointers on how to integrate a pre-existing mySQL database with a rails app. 如果有人能告诉我错误的出处或对如何将现有的mySQL数据库与Rails应用程序集成提供一般性指示,我将不胜感激。

Running rake db:migrate yields this error 运行rake db:migrate会产生此错误

== 1 CreateDatabase: migrating ================================================
-- create_table("Bill", {:primary_key=>"confirmationNo", :id=>:integer, :force=>:cascade, :options=>"ENGINE=InnoDB DEFAULT CHARSET=latin1"})
   -> 0.0556s
-- create_table("Customer", {:primary_key=>"creditCardNo", :id=>:integer, :force=>:cascade, :options=>"ENGINE=InnoDB DEFAULT CHARSET=latin1"})
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails: DROP TABLE `Customer` CASCADE

Here are my schema.rb and migrate/001_create_database.rb 这是我的schema.rb和migration / 001_create_database.rb

schema.rb schema.rb

ActiveRecord::Schema.define(version: 0) do

  create_table "Bill", primary_key: "confirmationNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "pointsEarned",            null: false
t.float   "amountDue",    limit: 24, null: false
  end

  create_table "Customer", primary_key: "creditCardNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "guestID",             null: false
t.string  "username", limit: 30
t.string  "name",     limit: 30, null: false
t.index ["guestID", "username"], name: "guestID", unique: true, using: :btree
t.index ["username"], name: "username", using: :btree
  end

  create_table "Manager", primary_key: "managerID", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "name", limit: 10, null: false
  end

  create_table "Reservation", primary_key: "confirmationNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.date    "inDate"
t.date    "outDate"
t.boolean "availability", null: false
t.integer "roomNo"
t.integer "creditCardNo"
t.index ["creditCardNo"], name: "creditCardNo", using: :btree
t.index ["inDate", "outDate"], name: "inDate", using: :btree
t.index ["roomNo"], name: "roomNo", using: :btree
  end

  create_table "Room", primary_key: "roomNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "roomType", limit: 30, null: false
t.index ["roomType"], name: "roomType", using: :btree
  end

  create_table "Roomtype", primary_key: "roomType", id: :string, limit: 30, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "capacity",            null: false
t.float   "rate",     limit: 24, null: false
  end

  create_table "Stay", primary_key: ["inDate", "outDate"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.date "inDate",  null: false
t.date "outDate", null: false
  end

  create_table "User", primary_key: "username", id: :string, limit: 1, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "points", null: false
  end

  add_foreign_key "Bill", "Reservation", column: "confirmationNo", primary_key: "confirmationNo", name: "Bill_ibfk_1"
  add_foreign_key "Customer", "User", column: "username", primary_key: "username", name: "Customer_ibfk_1"
  add_foreign_key "Reservation", "Customer", column: "creditCardNo", primary_key: "creditCardNo", name: "Reservation_ibfk_3"
  add_foreign_key "Reservation", "Room", column: "roomNo", primary_key: "roomNo", name: "Reservation_ibfk_1"
  add_foreign_key "Reservation", "Stay", column: "inDate", primary_key: "inDate", name: "Reservation_ibfk_2", on_delete: :cascade
  add_foreign_key "Reservation", "Stay", column: "outDate", primary_key: "outDate", name: "Reservation_ibfk_2", on_delete: :cascade
  add_foreign_key "Room", "Roomtype", column: "roomType", primary_key: "roomType", name: "Room_ibfk_1"
end

migrate/001_create_database.rb 迁移/ 001_create_database.rb

class CreateDatabase < ActiveRecord::Migration
  def self.up

ActiveRecord::Schema.define(version: 0) do

  create_table "Bill", primary_key: "confirmationNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "pointsEarned",            null: false
t.float   "amountDue",    limit: 24, null: false
  end

  create_table "Customer", primary_key: "creditCardNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "guestID",             null: false
t.string  "username", limit: 30
t.string  "name",     limit: 30, null: false
t.index ["guestID", "username"], name: "guestID", unique: true, using: :btree
t.index ["username"], name: "username", using: :btree
  end

  create_table "Manager", primary_key: "managerID", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "name", limit: 10, null: false
  end

  create_table "Reservation", primary_key: "confirmationNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.date    "inDate"
t.date    "outDate"
t.boolean "availability", null: false
t.integer "roomNo"
t.integer "creditCardNo"
t.index ["creditCardNo"], name: "creditCardNo", using: :btree
t.index ["inDate", "outDate"], name: "inDate", using: :btree
t.index ["roomNo"], name: "roomNo", using: :btree
  end

  create_table "Room", primary_key: "roomNo", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "roomType", limit: 30, null: false
t.index ["roomType"], name: "roomType", using: :btree
  end

  create_table "Roomtype", primary_key: "roomType", id: :string, limit: 30, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "capacity",            null: false
t.float   "rate",     limit: 24, null: false
  end

  create_table "Stay", primary_key: ["inDate", "outDate"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.date "inDate",  null: false
t.date "outDate", null: false
  end

  create_table "User", primary_key: "username", id: :string, limit: 1, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "points", null: false
  end

  add_foreign_key "Bill", "Reservation", column: "confirmationNo", primary_key: "confirmationNo", name: "Bill_ibfk_1"
  add_foreign_key "Customer", "User", column: "username", primary_key: "username", name: "Customer_ibfk_1"
  add_foreign_key "Reservation", "Customer", column: "creditCardNo", primary_key: "creditCardNo", name: "Reservation_ibfk_3"
  add_foreign_key "Reservation", "Room", column: "roomNo", primary_key: "roomNo", name: "Reservation_ibfk_1"
  add_foreign_key "Reservation", "Stay", column: "inDate", primary_key: "inDate", name: "Reservation_ibfk_2", on_delete: :cascade
  add_foreign_key "Reservation", "Stay", column: "outDate", primary_key: "outDate", name: "Reservation_ibfk_2", on_delete: :cascade
  add_foreign_key "Room", "Roomtype", column: "roomType", primary_key: "roomType", name: "Room_ibfk_1"
end

  end

  def self.down
# drop all the tables if you really need
# to support migration back to version 0
  end
end

Cheers 干杯

Does your MySQL Database have a "schema_migrations" table? 您的MySQL数据库是否有“ schema_migrations”表? Rails relies on this table to decide if there are any migrations pending based on the version number in the database and schema.rb file. Rails依靠此表根据数据库和schema.rb文件中的版本号来确定是否有任何待处理的迁移。 Update your schema.rb file so the version is in the standard format: "20170326120700" instead of "0" and then create a table "schema_migrations" with a field called "version" and insert the version number "20170326120700" into the new table. 更新您的schema.rb文件,使其版本为标准格式:“ 20170326120700”而不是“ 0”,然后创建一个名为“ version”的表“ schema_migrations”,并将版本号“ 20170326120700”插入新表。 Rails should stop complaining about the pending migration. Rails应该停止抱怨即将进行的迁移。 The "version" field should be type VARCHAR(255). “版本”字段应为VARCHAR(255)类型。

To bring the schema.rb in sync with the database again you can follow the process to recreate the schema.rb like you found in the tutorial "rake db:schema:dump". 要使schema.rb与数据库再次同步,可以按照重新创建schema.rb的过程进行操作,就像在教程“ rake db:schema:dump”中找到的那样。 Verify your versions still match up in the schema.rb file and "schema_migrations" table. 在schema.rb文件和“ schema_migrations”表中验证您的版本是否仍然匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM