Migration to create table raises Mysql2::Error: Table doesn't exist

前端 未结 4 830
刺人心
刺人心 2020-12-30 01:17

I wrote a migration with the following:

class CreateTableSomeTable < ActiveRecord::Migration[5.1]
  def change
    create_table :some_tables do |t|
               


        
4条回答
  •  Happy的楠姐
    2020-12-30 02:03

    I figured out a work around, but it is still very puzzling to me.

    The error message in the log file was not exactly pointing to the issue. For some reason, it might be rails 5.1.1 or it might be mysql2 0.4.6, but it doesn't like using references within the create_table block for some reason. Very odd because it has worked for me in the past.

    So I changed the migration from this:

    class CreateTableSomeTable < ActiveRecord::Migration[5.1]
      def change
        create_table :some_tables do |t|
          t.references :user, foreign_key: true
          t.references :author, references: :user, foreign_key: true
          t.text :summary
        end
      end
    end
    

    To this:

    class CreateTableSomeTable < ActiveRecord::Migration[5.1]
      def change
        create_table :some_tables do |t|
          t.integer :user_id
          t.integer :author_id
          t.text :summary
        end
      end
    end
    

    And it worked.

    It is very odd because references works just fine with sqlite3 (I tested this by generating a dummy app, ran a scaffold command with a references column, and ran rails db:migrate and it all worked).

提交回复
热议问题