运行rake db:seed时Heroku回滚

时间:2017-01-05 17:16:05

标签: ruby-on-rails postgresql heroku deployment

我一直在寻找一个解决方案,我发现有些人喜欢把ruby版本放在你的gemfile中,以免在播种时遇到回滚错误等等。但是,没有任何效果。这是我第三次部署到Heroku的应用程序,这是我第一次遇到以下错误:

User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.4ms)  ROLLBACK
   (0.4ms)  BEGIN
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.4ms)  ROLLBACK
   (0.4ms)  BEGIN
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.4ms)  ROLLBACK
   (0.4ms)  BEGIN
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.4ms)  ROLLBACK
   (0.7ms)  BEGIN
  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "b@gmail.com"], ["LIMIT", 1]]
  SQL (1.0ms)  INSERT INTO "users" ("id", "email", "encrypted_password", "created_at", "updated_at", "username") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["id", 1], ["email", "b@gmail.com"], ["encrypted_password", "$2a$11$K.dh0OhopIOVZgT..0Yi7ukDGcw/IHmCAmZaH7LuHWZnfbJLaSWzS"], ["created_at", 2017-01-05 17:07:23 UTC], ["updated_at", 2017-01-05 17:07:23 UTC], ["username", "paco"]]
   (1.3ms)  COMMIT

我有一个带有654321密码的用户名作为一个例子,正如你在倒数第二行看到的那样加密。

这是我的架构:



ActiveRecord::Schema.define(version: 20161231124005) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "bids", force: :cascade do |t|
    t.integer  "amount"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "product_id"
    t.index ["product_id"], name: "index_bids_on_product_id", using: :btree
    t.index ["user_id"], name: "index_bids_on_user_id", using: :btree
  end

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "products", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.string   "image_url"
    t.integer  "price"
    t.datetime "deadline"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
    t.integer  "category_id"
  end

  create_table "ratings", force: :cascade do |t|
    t.integer  "rating"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "product_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    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.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "username"
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
    t.index ["username"], name: "index_users_on_username", unique: true, using: :btree
  end

  add_foreign_key "bids", "products"
  add_foreign_key "bids", "users"
end




这些是我的种子,我试图给出一个随机的用户名(因为我添加了该列来设计gem):



category1 = Category.create(name: "computers")
category2 = Category.create(name: "mobile")
category3 = Category.create(name: "clothes")
category4 = Category.create(name: "accesories")

Product.create(category: category1, title: "McBook", description: "Best PC for developers", price: 500,image_url: "mc-book.jpg", user_id: 1, deadline: "12/11/2016")
Product.create(category: category3,title: "Winter jacket", description: "Keep it warm even in wintertime", price: 40,image_url: "winter-jacket.png", user_id: 1, deadline: "18/04/2016")
Product.create(category: category4,title: "Rayban sunglasses", description: "Cool sunglasses for summertime", price: 60,image_url: "rayban.jpg", user_id: 1, deadline: "09/12/2016")
Product.create(category: category3,title: "Casual Jacket", description: "Cool jacket", price: 20,image_url: "jacket.jpg", user_id: 1, deadline: "09/12/2016")
Product.create(category: category1,title: "HP computer", description: "Ultimate computer", price: 300,image_url: "hp.png", user_id: 1, deadline: "09/12/2016")
Product.create(category: category4,title: "Normal sunglasses", description: "Geeky glasses", price: 10,image_url: "sunglasses.png", user_id: 1, deadline: "09/12/2016")

10.times do |index|
  User.create(username: "user-#{ SecureRandom.hex(10)}", email: "email#{index}@example.com", password: 'password123#')
end




2 个答案:

答案 0 :(得分:0)

这是因为数据库中已存在具有相同电子邮件("b@gmail.com")的用户,我认为您在email上有唯一性验证

User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "b@gmail.com"], ["LIMIT", 1]]

使用类似此类的序列更改用户的电子邮件

10.times do |index|
  User.create(email: "email#{index}@example.com", password: 'password123#')
end

这将创建10个具有不同电子邮件的用户

email0@example.com
email1@example.com
email2@example.com
email3@example.com
email4@example.com
email5@example.com
email6@example.com
email7@example.com
email8@example.com
email9@example.com

答案 1 :(得分:0)

我做到了。我在模型中键入以下内容:

class User < ApplicationRecord
	has_many :products
	has_many :ratings
	has_many :bids
	validates :email, presence: true, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i },
    uniqueness:  { case_sensitive: false }
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

然后,我不知道为什么我移动到Postgres时user_id和产品之间的关系给了我错误所以我做了:rails g migration AddUserToProducts user:references。

然后使用以下命令将数据库放入生产中:heroku pg:reset DATABASE

更改以下种子:

category1 = Category.create(name: "computers")
category2 = Category.create(name: "mobile")
category3 = Category.create(name: "clothes")
category4 = Category.create(name: "accesories")

Product.create(category: category1, title: "McBook", description: "Best PC for developers", price: 500,image_url: "mc-book.jpg", user_id: 2, deadline: "12/11/2016")
Product.create(category: category3,title: "Winter jacket", description: "Keep it warm even in wintertime", price: 40,image_url: "winter-jacket.png", user_id: 2, deadline: "18/04/2016")
Product.create(category: category4,title: "Rayban sunglasses", description: "Cool sunglasses for summertime", price: 60,image_url: "rayban.jpg", user_id: 2, deadline: "09/12/2016")
Product.create(category: category3,title: "Casual Jacket", description: "Cool jacket", price: 20,image_url: "jacket.jpg", user_id: 2, deadline: "09/12/2016")
Product.create(category: category1,title: "HP computer", description: "Ultimate computer", price: 300,image_url: "hp.png", user_id: 2, deadline: "09/12/2016")
Product.create(category: category4,title: "Normal sunglasses", description: "Geeky glasses", price: 10,image_url: "sunglasses.png", user_id: 2, deadline: "09/12/2016")

User.create(id: 2, email: "b@gmail.com", username: "paco", password: "653412")

最后运行heroku运行rake db:migrate和heroku运行rake db:seed。

这次正在保存产品,尽管用户在种子中以用户身份登录不起作用。所以我签了另一个用户(在前端)并将产品分配给他。