【问题标题】:Rake:db migrate failing with pre-existing mySQL databaseRake:db migrate 使用预先存在的 mySQL 数据库失败
【发布时间】:2017-03-26 16:34:35
【问题描述】:

我正在尝试使用预先存在的 mySQL 数据库创建一个 rails 实例,我正在关注this guide

步骤 1-3 有效,但在运行 rails 服务器时,它说有待迁移。

如果有人能告诉我错误出在哪里,或者就如何将预先存在的 mySQL 数据库与 rails 应用程序集成提供一些一般性指示,我将不胜感激。

运行 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

这是我的 schema.rb 和 migrate/001_create_database.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

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

干杯

【问题讨论】:

    标签: mysql ruby-on-rails rake rails-migrations


    【解决方案1】:

    您的 MySQL 数据库是否有“schema_migrations”表? Rails 依靠这个表来根据数据库和 schema.rb 文件中的版本号来决定是否有任何迁移未决。更新您的 schema.rb 文件,使版本为标准格式:“20170326120700”而不是“0”,然后创建一个表“schema_migrations”,其中包含一个名为“version”的字段,并将版本号“20170326120700”插入到新表中. Rails 应该停止抱怨未决的迁移。 “版本”字段应为 VARCHAR(255) 类型。

    要使 schema.rb 再次与数据库同步,您可以按照教程“rake db:schema:dump”中的流程重新创建 schema.rb。验证您的版本在 schema.rb 文件和“schema_migrations”表中是否仍然匹配。

    【讨论】:

    • 感谢您的帮助!我试过这个,不幸的是它没有用。有没有什么我没有包含在我的原始帖子中可以用来帮助诊断问题的内容?
    猜你喜欢
    • 2018-11-25
    • 2021-08-25
    • 2017-01-20
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多