简体   繁体   中英

Spree Could not find table 'spree_countries'

I have cloned a existing Spree project on Rails 3.2.17.

Rake db:create works but when running migrate, seed or rails s I run into this error.

Could not find table 'spree_countries'

I have tried

rake db:reset

and various spree generators but all give back the same error.

The Error

This is the error I got when like OP I did anything with Rake:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  
  relation "spree_countries" does not exist

The Problem

The most common solution for setting the country in Spree is the following:

config/initializers/spree.rb

Spree.config do |config|
  country = Spree::Country.find_by_name('Mexico')
  config.default_country_id = country.id if country.present?
end

This will work fine until you drop the database and try to recreate it - and do not overwrite config/initializers/spree.rb .

Overwrite .../config/initializers/spree.rb? (enter "h" for help) [Ynaqdh] n

While recreating the database the Country table is not available and you hit the Could not find table 'spree_countries' error. It's nasty because the initialization code that causes the bug works for a while and it is only much later the error arises when you decide to drop the database.

The Solution

The code suggested on google groups was to find the country_id and assign it - you can puts the id out or use an SQL query on spree_countries .

config/initializers/spree.rb

Spree.config do |config|
  # Sets default country to Mexico
  config.default_country_id = 157
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM