迁移期间Rails外键约束问题

时间:2018-06-10 01:41:07

标签: ruby-on-rails

在rake db:migration:

期间遇到问题
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 IF EXISTS `artists` CASCADE

首次运行rake db:reset后会发生这种情况。似乎与外键约束相关但不确定如何修复迁移文件。有什么想法吗?

连接到现有的MySQL数据库,并建议根据我的架构转储创建迁移文件。

迁移文件如下所示:

class CreateDatabaseStructure < ActiveRecord::Migration[5.1]
  def change
    create_table "artists", id: :bigint, unsigned: true, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.text "name"
      t.text "gender"
      t.text "pr"
      t.integer "most_streamed"
      t.integer "monthly_listeners"
      t.text "uri"
      t.datetime "latest_release"
      t.datetime "latest_update"
      t.text "source"
      t.datetime "added_to_database"
      t.text "artwork_url"
      t.text "notes"
      t.text "instagram"
      t.index ["id"], name: "id", unique: true
    end

    create_table "artists_genres", primary_key: ["artist_id", "genre_id"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.bigint "artist_id", null: false, unsigned: true
      t.integer "genre_id", null: false
      t.index ["genre_id"], name: "fk_Artist_Genre__genres"
    end

    create_table "artists_labels", primary_key: ["artist_id", "label_id"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.bigint "artist_id", null: false, unsigned: true
      t.integer "label_id", null: false
      t.index ["label_id"], name: "fk_Artist_Label__labels"
    end

    create_table "artists_playlists", primary_key: ["artist_id", "playlist_id"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.bigint "artist_id", null: false, unsigned: true
      t.integer "playlist_id", null: false
      t.index ["playlist_id"], name: "fk_Artist_Playlist__playlists"
    end

    create_table "artists_songs", primary_key: ["artist_id", "song_id"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.bigint "artist_id", null: false, unsigned: true
      t.integer "song_id", null: false
      t.index ["song_id"], name: "fk_Artist_Song__songs"
    end

    create_table "creators", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.string "name"
      t.string "type"
    end

    create_table "creators_songs", primary_key: ["creator_id", "song_id"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.integer "creator_id", null: false
      t.integer "song_id", null: false
      t.index ["song_id"], name: "fk_Creator_Song__songs"
    end

    create_table "data", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
      t.string "playlist_name"
      t.text "playlist_uri"
      t.text "playlist_user"
    end

    create_table "genres", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.string "name"
    end

    create_table "labels", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.string "name"
    end

    create_table "playlists", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
      t.string "playlist_name"
      t.text "playlist_uri"
      t.text "playlist_user"
      t.text "playlist_check"
      t.integer "userID", null: false
      t.integer "playlist_followers"
    end

    create_table "songs", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.string "name"
      t.string "song_uri"
    end

    create_table "users", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
      t.string "name"
    end

    add_foreign_key "artists_genres", "artists", name: "fk_Artist_Genre__artist"
    add_foreign_key "artists_genres", "genres", name: "fk_Artist_Genre__genres"
    add_foreign_key "artists_labels", "artists", name: "fk_Artist_Label__artist"
    add_foreign_key "artists_labels", "labels", name: "fk_Artist_Label__labels"
    add_foreign_key "artists_playlists", "artists", name: "fk_Artist_Playlist__artist"
    add_foreign_key "artists_playlists", "playlists", name: "fk_Artist_Playlist__playlists"
    add_foreign_key "artists_songs", "artists", name: "fk_Artist_Song__artist"
    add_foreign_key "artists_songs", "songs", name: "fk_Artist_Song__songs"
    add_foreign_key "creators_songs", "creators", name: "fk_Creator_Song__creators"
    add_foreign_key "creators_songs", "songs", name: "fk_Creator_Song__songs"
  end

end

0 个答案:

没有答案
相关问题