运行 次迁移时出现问题

Spree issue when running migrations

我正在尝试学习如何使用 Rails 和 Spree 构建电子商务网站。我正在按照教程创建我的 Rails 应用程序,然后:

$ gem install spree
$ spree install

在那之后我被要求 运行

$ rake db:migrate

我这样做了,但出现以下错误:

    `$ rake db:migrate
[WARNING] You are not setting Devise.secret_key within your application!
You must set this in config/initializers/devise.rb. Here's an example:

Devise.secret_key = "fe7e0f9349df1fbb68a26182598c3114a2e54376d92768e47dce50ec5c6535de6a7d7e40d1051a7698a4b187d01ee4f4f968"
== 20150322040605 SpreeOneTwo: migrating ======================================
-- table_exists?(:spree_addresses)
   -> 0.0007s
-- create_table(:spree_activators)
   -> 0.0075s
-- create_table(:spree_addresses)
   -> 0.0029s
-- add_index(:spree_addresses, [:firstname], {:name=>"index_addresses_on_firstname"})
   -> 0.0023s
-- add_index(:spree_addresses, [:lastname], {:name=>"index_addresses_on_lastname"})
   -> 0.0019s
-- create_table(:spree_adjustments)
   -> 0.0034s
-- add_index(:spree_adjustments, [:adjustable_id], {:name=>"index_adjustments_on_order_id"})
   -> 0.0021s
-- create_table(:spree_assets)
   -> 0.0030s
-- add_index(:spree_assets, [:viewable_id], {:name=>"index_assets_on_viewable_id"})
   -> 0.0020s
-- add_index(:spree_assets, [:viewable_type, :type], {:name=>"index_assets_on_viewable_type_and_type"})
   -> 0.0019s
-- create_table(:spree_calculators)
   -> 0.0025s
-- create_table(:spree_configurations)
   -> 0.0025s
-- add_index(:spree_configurations, [:name, :type], {:name=>"index_spree_configurations_on_name_and_type"})
   -> 0.0031s
-- create_table(:spree_countries)
   -> 0.0027s
-- create_table(:spree_credit_cards)
   -> 0.0030s
-- create_table(:spree_gateways)
   -> 0.0036s
-- create_table(:spree_inventory_units)
   -> 0.0030s
-- add_index(:spree_inventory_units, [:order_id], {:name=>"index_inventory_units_on_order_id"})
   -> 0.0020s
-- add_index(:spree_inventory_units, [:shipment_id], {:name=>"index_inventory_units_on_shipment_id"})
   -> 0.0032s
-- add_index(:spree_inventory_units, [:variant_id], {:name=>"index_inventory_units_on_variant_id"})
   -> 0.0022s
-- create_table(:spree_line_items)
   -> 0.0023s
-- add_index(:spree_line_items, [:order_id], {:name=>"index_spree_line_items_on_order_id"})
   -> 0.0021s
-- add_index(:spree_line_items, [:variant_id], {:name=>"index_spree_line_items_on_variant_id"})
   -> 0.0020s
-- create_table(:spree_log_entries)
   -> 0.0027s
-- create_table(:spree_mail_methods)
   -> 0.0032s
-- create_table(:spree_option_types)
   -> 0.0027s
-- create_table(:spree_option_types_prototypes, {:id=>false})
   -> 0.0009s
-- create_table(:spree_option_values)
   -> 0.0029s
-- create_table(:spree_option_values_variants, {:id=>false})
   -> 0.0007s
-- add_index(:spree_option_values_variants, [:variant_id, :option_value_id], {:name=>"index_option_values_variants_on_variant_id_and_option_value_id"})
   -> 0.0022s
-- add_index(:spree_option_values_variants, [:variant_id], {:name=>"index_spree_option_values_variants_on_variant_id"})
   -> 0.0020s
-- create_table(:spree_orders)
   -> 0.0048s
-- add_index(:spree_orders, [:number], {:name=>"index_spree_orders_on_number"})
   -> 0.0029s
-- create_table(:spree_payment_methods)
   -> 0.0035s
-- create_table(:spree_payments)
   -> 0.0034s
-- create_table(:spree_preferences)
   -> 0.0031s
-- add_index(:spree_preferences, [:key], {:name=>"index_spree_preferences_on_key", :unique=>true})
   -> 0.0022s
-- create_table(:spree_product_option_types)
   -> 0.0021s
-- create_table(:spree_product_properties)
   -> 0.0028s
-- add_index(:spree_product_properties, [:product_id], {:name=>"index_product_properties_on_product_id"})
   -> 0.0029s
-- create_table(:spree_products)
   -> 0.0037s
-- add_index(:spree_products, [:available_on], {:name=>"index_spree_products_on_available_on"})
   -> 0.0022s
-- add_index(:spree_products, [:deleted_at], {:name=>"index_spree_products_on_deleted_at"})
   -> 0.0020s
-- add_index(:spree_products, [:name], {:name=>"index_spree_products_on_name"})
   -> 0.0030s
-- add_index(:spree_products, [:permalink], {:name=>"index_spree_products_on_permalink"})
   -> 0.0026s
-- create_table(:spree_products_taxons, {:id=>false})
   -> 0.0008s
-- add_index(:spree_products_taxons, [:product_id], {:name=>"index_spree_products_taxons_on_product_id"})
   -> 0.0021s
-- add_index(:spree_products_taxons, [:taxon_id], {:name=>"index_spree_products_taxons_on_taxon_id"})
   -> 0.0021s
-- create_table(:spree_properties)
   -> 0.0032s
-- create_table(:spree_properties_prototypes, {:id=>false})
   -> 0.0006s
-- create_table(:spree_prototypes)
   -> 0.0027s
-- create_table(:spree_return_authorizations)
   -> 0.0032s
-- create_table(:spree_roles)
   -> 0.0026s
-- create_table(:spree_roles_users, {:id=>false})
   -> 0.0006s
-- add_index(:spree_roles_users, [:role_id], {:name=>"index_spree_roles_users_on_role_id"})
   -> 0.0021s
-- add_index(:spree_roles_users, [:user_id], {:name=>"index_spree_roles_users_on_user_id"})
   -> 0.0034s
-- create_table(:spree_shipments)
   -> 0.0032s
-- add_index(:spree_shipments, [:number], {:name=>"index_shipments_on_number"})
   -> 0.0024s
-- create_table(:spree_shipping_categories)
   -> 0.0038s
-- create_table(:spree_shipping_methods)
   -> 0.0039s
-- create_table(:spree_state_changes)
   -> 0.0032s
-- create_table(:spree_states)
   -> 0.0028s
-- create_table(:spree_tax_categories)
   -> 0.0033s
-- create_table(:spree_tax_rates)
   -> 0.0024s
-- create_table(:spree_taxonomies)
   -> 0.0029s
-- create_table(:spree_taxons)
   -> 0.0038s
-- add_index(:spree_taxons, [:parent_id], {:name=>"index_taxons_on_parent_id"})
   -> 0.0023s
-- add_index(:spree_taxons, [:permalink], {:name=>"index_taxons_on_permalink"})
   -> 0.0027s
-- add_index(:spree_taxons, [:taxonomy_id], {:name=>"index_taxons_on_taxonomy_id"})
   -> 0.0034s
-- create_table(:spree_tokenized_permissions, {:force=>true})
   -> 0.0038s
-- add_index(:spree_tokenized_permissions, [:permissable_id, :permissable_type], {:name=>"index_tokenized_name_and_type"})
   -> 0.0023s
-- create_table(:spree_trackers)
   -> 0.0031s
-- create_table(:spree_users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "spree_users" already exists
: CREATE TABLE "spree_users" ("id" serial primary key, "encrypted_password" character varying(128), "password_salt" character varying(128), "email" character varying, "remember_token" character varying, "persistence_token" character varying, "reset_password_token" character varying, "perishable_token" character varying, "sign_in_count" integer DEFAULT 0 NOT NULL, "failed_attempts" integer DEFAULT 0 NOT NULL, "last_request_at" timestamp, "current_sign_in_at" timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" character varying, "last_sign_in_ip" character varying, "login" character varying, "ship_address_id" integer, "bill_address_id" integer, "authentication_token" character varying, "unlock_token" character varying, "locked_at" timestamp, "remember_created_at" timestamp, "reset_password_sent_at" timestamp, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL) /Users/luisjaramillo/Repos/Carrolujos/db/migrate/20150322040605_spree_one_two.spree.rb:425:in `up'
ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR:  relation "spree_users" already exists
: CREATE TABLE "spree_users" ("id" serial primary key, "encrypted_password" character varying(128), "password_salt" character varying(128), "email" character varying, "remember_token" character varying, "persistence_token" character varying, "reset_password_token" character varying, "perishable_token" character varying, "sign_in_count" integer DEFAULT 0 NOT NULL, "failed_attempts" integer DEFAULT 0 NOT NULL, "last_request_at" timestamp, "current_sign_in_at" timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" character varying, "last_sign_in_ip" character varying, "login" character varying, "ship_address_id" integer, "bill_address_id" integer, "authentication_token" character varying, "unlock_token" character varying, "locked_at" timestamp, "remember_created_at" timestamp, "reset_password_sent_at" timestamp, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL) 
/Users/luisjaramillo/Repos/Carrolujos/db/migrate/20150322040605_spree_one_two.spree.rb:425:in `up'
PG::DuplicateTable: ERROR:  relation "spree_users" already exists
/Users/luisjaramillo/Repos/Carrolujos/db/migrate/20150322040605_spree_one_two.spree.rb:425:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)`

我真的不知道如何解决这个问题。我将非常感谢有人的帮助

表示"spree_users"table已经存在。您是否在迁移之外手动创建了 table?如果是这样,最简单的事情就是再次删除 table 和 运行 迁移。如果您只是按照教程学习,您也可以只执行 rake db:reset 这将删除您的数据库,重新创建它,然后 运行 进行迁移。

看起来您已经有一个 spree_users table。给您带来麻烦的迁移是在 Spree 决定为所有内容命名空间时添加到 Spree 的迁移。如果你打开 db/migrate/20150218150613_spree_one_two.spree.rb 你会在第 425 行附近找到这段代码:

create_table :spree_users do |t|
  t.string     :encrypted_password,     :limit => 128
  t.string     :password_salt,          :limit => 128
  t.string     :email
  t.string     :remember_token
  t.string     :persistence_token
  t.string     :reset_password_token
  t.string     :perishable_token
  t.integer    :sign_in_count,                         :default => 0, :null => false
  t.integer    :failed_attempts,                       :default => 0, :null => false
  t.datetime   :last_request_at
  t.datetime   :current_sign_in_at
  t.datetime   :last_sign_in_at
  t.string     :current_sign_in_ip
  t.string     :last_sign_in_ip
  t.string     :login
  t.references :ship_address
  t.references :bill_address
  t.string     :authentication_token
  t.string     :unlock_token
  t.datetime   :locked_at
  t.datetime   :remember_created_at
  t.datetime   :reset_password_sent_at
  t.timestamps null: false
end

只需将代码包装在检查 spree_users 列的 if 语句中,如下所示:

unless table_exists? :spree_users

  create_table :spree_users do |t|
    t.string     :encrypted_password,     :limit => 128
    t.string     :password_salt,          :limit => 128
    t.string     :email
    t.string     :remember_token
    t.string     :persistence_token
    t.string     :reset_password_token
    t.string     :perishable_token
    t.integer    :sign_in_count,                         :default => 0, :null => false
    t.integer    :failed_attempts,                       :default => 0, :null => false
    t.datetime   :last_request_at
    t.datetime   :current_sign_in_at
    t.datetime   :last_sign_in_at
    t.string     :current_sign_in_ip
    t.string     :last_sign_in_ip
    t.string     :login
    t.references :ship_address
    t.references :bill_address
    t.string     :authentication_token
    t.string     :unlock_token
    t.datetime   :locked_at
    t.datetime   :remember_created_at
    t.datetime   :reset_password_sent_at
    t.timestamps null: false
  end

end