【问题标题】:Heroku rollback when running rake db:seed运行 rake db:seed 时 Heroku 回滚
【发布时间】:2017-01-05 17:16:04
【问题描述】:

我一直在寻找解决这个问题的方法,我发现有些类似将 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

【问题讨论】:

    标签: ruby-on-rails postgresql heroku deployment


    【解决方案1】:

    这是因为具有相同电子邮件("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
    

    【讨论】:

    • 不,我遇到了同样的错误“回滚”。我的应用程序在 localhost 中运行正常,但是在部署时我发现 heroku 运行 rake db:seed 时出现错误
    • 如果我在没有 heroku 的情况下运行 rake db:seed,我会得到以下信息: ActiveRecord::RecordNotUnique: PG::UniqueViolation: 错误:重复键值违反唯一约束“users_pkey” 详细信息:键 (id)= (2) 已经存在。 : INSERT INTO "users" ("email", "encrypted_pa​​ssword", "created_at", "updated_at", "username") VALUES ($1, $2, $3, $4, $5) RETURNING "id" 但是,我不明白为什么我在那里得到“encrypted_pa​​ssword”...
    【解决方案2】:

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

    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 from 和产品之间的关系给了我错误,所以我这样做了: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 run rake db:migrate 和 heroku run rake db:seed。

    这一次产品被保存了,虽然用户在种子中的用户登录形式不起作用。所以我注册了另一个用户(在前端)并将产品分配给他。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2019-10-05
      • 2012-11-17
      • 2014-12-26
      相关资源
      最近更新 更多