Home Rails Mysql2::Error Table doesn't exist When create new migration
Reply: 0

Rails Mysql2::Error Table doesn't exist When create new migration

user4890
1#
user4890 Published in September 19, 2018, 9:48 am

I wrote a migration with the following (create new table named sources):

class CreateSources < ActiveRecord::Migration
  def change
    create_table :sources do |t|
      t.string :name, null: false, default: ""

      t.timestamps null: false
    end
  end
end

And then I modified my existing model :

class Property < ActiveRecord::Base
  validates :source, allow_blank: true, inclusion: { in: 
  Source.all.map{ |source| source.name } }

I want to add validation to the property's source to only allow source from sources table.

And then when I run the migration, I got the following error:

ActiveRecord::StatementInvalid: Mysql2::Error: Table 'sources' doesn't exist: SELECT `sources`.* FROM `sources`

The problem is query of source table is occured when it hasn't been initialized yet.
Any tips on how I can get the migration to run?
This is run on production level. so I might can't drop all the migration and rearrange it.

Rails version 4.2.5
SQL version 5.7

share|improve this question

2 Answers 2

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.510393 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO