【问题标题】:Rails db:seed not populating postgresql on Ubuntu 16.04Rails db:seed 未在 Ubuntu 16.04 上填充 postgresql
【发布时间】:2019-01-05 11:29:49
【问题描述】:

我有一个在 Ubuntu 16.04 服务器上运行的 Rails 应用程序。据我所知,似乎一切都设置正确

  • 我创建了一个名为“rails”的 psql 用户
  • 我创建了myapp_name_production + _development & _test,所有者设置为rails(我的Ubuntu用户)
  • 我跑了rails db:createrails db:migrate
  • 我运行了rails db:seed,但它只是延迟了一会儿,然后才返回到新的命令行。

我像这样配置了我的database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  host: localhost
  username: postgres
  password: mypassword

development:
  <<: *default
  database: myapp_name_development

production:
  <<: *default
  database: myapp_name_production

我的seeds.rb 文件:

news = BlogCategory.create name: "News"
reviews = BlogCategory.create name: "Reviews"
interviews = BlogCategory.create name: "Interviews"
tutorials = BlogCategory.create name: "Tutorials"
noCat = BlogCategory.create name: "No Category"

subcat = BlogCategory.create name: "Audio Industry", parent_id: news.id
subcat = BlogCategory.create name: "Game Audio", parent_id: news.id
subcat = BlogCategory.create name: "Film Audio", parent_id: news.id

subcat = BlogCategory.create name: "Software", parent_id: reviews.id
subcat = BlogCategory.create name: "Hardware", parent_id: reviews.id

subcat = BlogCategory.create name: "Sound Designers", parent_id: interviews.id
subcat = BlogCategory.create name: "Composers", parent_id: interviews.id
subcat = BlogCategory.create name: "Game Developers", parent_id: interviews.id
subcat = BlogCategory.create name: "Voice Talent", parent_id: interviews.id

subcat = BlogCategory.create name: "Sound Design", parent_id: tutorials.id
subcat = BlogCategory.create name: "Composition", parent_id: tutorials.id
subcat = BlogCategory.create name: "Implementation", parent_id: tutorials.id
subcat = BlogCategory.create name: "Voice Acting", parent_id: tutorials.id

Admin = User.create username: "Admin", first_name: "Admin", email: "[email@hotmail.com]", admin: true, password: "[password]", activated: true, slug: "admin"

我不太确定我还需要做什么,我错过了什么吗?

我还验证了这些表是通过执行以下操作创建的:

$ psql -d myapp_name_production
psql (9.5.13)
Type "help" for help.

myapp_name_production=> \d
                  List of relations
 Schema |           Name           |   Type   | Owner
--------+--------------------------+----------+-------
 public | ar_internal_metadata     | table    | rails
 public | blog_categories          | table    | rails
 public | blog_categories_id_seq   | sequence | rails
 public | ckeditor_assets          | table    | rails
 public | ckeditor_assets_id_seq   | sequence | rails
 public | comments                 | table    | rails
 public | comments_id_seq          | sequence | rails
 public | contacts                 | table    | rails
 public | contacts_id_seq          | sequence | rails
 public | friendly_id_slugs        | table    | rails
 public | friendly_id_slugs_id_seq | sequence | rails
 public | newsletters              | table    | rails
 public | newsletters_id_seq       | sequence | rails
 public | packs                    | table    | rails
 public | packs_id_seq             | sequence | rails
 public | posts                    | table    | rails
 public | posts_id_seq             | sequence | rails
 public | purchases                | table    | rails
 public | purchases_id_seq         | sequence | rails
 public | schema_migrations        | table    | rails
 public | users                    | table    | rails
 public | users_id_seq             | sequence | rails
(22 rows)

所有这些都在我的 localhost:3000 上的计算机(Windows)上运行。

更新:

我的 BlogCategory 模型:

class BlogCategory < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged
  has_many :posts

  # This is called a self referential relation. This is where records in a table may point to other records in the same table.
  has_many :sub_categories, class_name: "BlogCategory", foreign_key: :parent_id
  has_many :sub_category_posts, through: :sub_categories, source: :posts
  belongs_to :parent, class_name: 'BlogCategory', foreign_key: :parent_id
  # This is a scope to load the top level categories and eager-load their posts, subcategories, and the subcategories' posts too.
  scope :top_level, -> { where(parent_id: nil).includes :posts, sub_categories: :posts }

  def should_generate_new_friendly_id?
    slug.nil? || name_changed?
  end

end

【问题讨论】:

  • 以防万一,你的文件在db/seeds.rb下吗?
  • 你能粘贴整个堆栈跟踪吗?
  • @AlexUnger 是的,该文件位于我的应用程序目录中的db/seeds.rb 下。 @jedi 我该怎么做才能获得堆栈跟踪?
  • 你设置RAILS_ENV使用你的生产环境了吗?
  • @jedi 或者只使用create! 而不是create

标签: ruby-on-rails ruby postgresql ubuntu


【解决方案1】:

你在你的模型中试过这个吗?:

belongs_to :parent, class_name: 'BlogCategory', foreign_key: :parent_id, optional: true

belongs_to 关联设为可选

【讨论】:

  • 我很确定这确实有效,我正在检查开发数据库,​​它在我更改后填充了生产数据库,但让我检查一些东西,看看它是否有效。
  • 谢谢,在重新创建了我的 Ubuntu 服务器后,我发现这确实是我的种子成功的原因。非常感谢@spickermann 以及他指出我必须使用rails db:seed RAILS_ENV='production' 来定义种子被发送到的位置。
【解决方案2】:

"ActiveRecord::RecordInvalid: Validation failed: Parent must exist" 表示可能前5行代码失败,这就是其他记录没有保存的原因。

试着把它放在friendly_id :name, use: :slugged 行之后

belongs_to :parent, class_name: 'BlogCategory', foreign_key: 'parent_id'

【讨论】:

  • 我已将我的模型包含在问题中。我还应该说我注释掉了除了第一个 create! 种子之外的所有内容,这就是我得到的错误。主要类别应该有parent_id NULL,所以我尝试不使用它,parent_id: NULLparent_id: nil 没有工作。
  • 你有什么版本的 Rails?
  • 我使用的是 Rails 5.1.6
猜你喜欢
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多