2017-03-26 2 views
0

기존 mySQL 데이터베이스로 레일스 인스턴스를 만들려고하고 있으며 this guide을 따르고 있습니다.Rake : 기존 mySQL 데이터베이스에서 db 마이그레이션 실패

1 ~ 3 단계가 작동했지만 레일 서버를 실행할 때 보류중인 마이그레이션이 있다고합니다.

누군가가 오류의 원인을 알 수 있거나 기존 mySQL 데이터베이스를 레일 응용 프로그램과 통합하는 방법에 대한 일반적인 지침을 제공하는 경우 매우 감사하게 생각합니다.

실행 레이크 dB : 여기

== 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을이 오류를 마이그레이션 및/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 

마이그레이션/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 

건배

답변

0

귀하의 MySQL 데이터베이스에 "schema_migrations"테이블이 있습니까? Rails는이 테이블을 사용하여 database 및 schema.rb 파일의 버전 번호를 기반으로 보류중인 마이그레이션이 있는지 결정합니다. 버전이 표준 형식 ("0"대신 "20170326120700")이되도록 schema.rb 파일을 업데이트 한 다음 "version"이라는 필드로 "schema_migrations"테이블을 만들고 새 테이블에 버전 번호 "20170326120700"을 삽입하십시오 . 레일즈는 보류중인 이전에 대해 불평하지 않아야한다. "버전"필드는 VARCHAR (255) 유형이어야합니다.

schema.rb를 데이터베이스와 다시 동기화하려면 튜토리얼 "rake db : schema : dump"에서 찾은 것처럼 schema.rb를 다시 만드는 프로세스를 따라야합니다. 버전이 schema.rb 파일과 "schema_migrations"테이블에서 여전히 일치하는지 확인하십시오.

+0

도움 주셔서 감사합니다. 나는 이것을 시도했지만 불행히도 효과가 없었다. 문제를 진단하는 데 사용할 수있는 원래 게시물에 포함되지 않은 것이 있습니까? – jfwerbes

+0

"마이그레이션 보류 중"오류가 사라졌습니까? – tronmcp

관련 문제