【问题标题】:Writing a seed file and getting syntax error and Null:Violation Error编写种子文件并出现语法错误和 Null:Violation 错误
【发布时间】:2023-01-23 00:57:04
【问题描述】:

我正在尝试编写一个将生成随机 Kudo 的种子文件。一个 Kudo 有一个标题,一个内容(都是用 Faker gem 生成的),但这些都不是问题。 Kudo 还有一个 giver_id 和一个 receiver_id 外键。我想让我的 Kudo 在不同的用户之间随机生成。而且我一直在盲目地尝试不同的语法(我是 Rails 的超级新手;))。所以我的种子文件看起来像这样

employees = Employee.create!([{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},...])

kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, Kudo.new(giver:Employee.create()), Kudo.new(receiver:Employee.create()))

这给了我语法错误

SyntaxError: /home/valar/Dokumenty/ERP_v1/db/seeds.rb:9: syntax error, unexpected ',', expecting => ...o.new(giver:Employee.create()), Kudo.new(receiver:Employee.c... ...                  /home/valar/Dokumenty/ERP_v1/db/seeds.rb:9: syntax error, unexpected ')', expecting end-of-input ...ew(receiver:Employee.create()) ... ... ...                            ^

我也试着让我的种子文件看起来像这样

kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, giver:Employee.create(), receiver:Employee.create())

但我不断收到 null:violation 错误

rake aborted!
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR:  null value in column "giver_id" violates not-null constraint

这是模型。员工

class Employee < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :given_kudos, class_name: 'Kudo', foreign_key: 'giver_id'
  has_many :received_kudos, class_name: 'Kudo', foreign_key: 'receiver_id'
end

还有工藤:

class Kudo < ApplicationRecord
  validates :Title, presence: true
  validates :Content, presence: true
  belongs_to :giver, class_name: 'Employee'
  belongs_to :receiver, class_name: 'Employee'
end

我的架构文件:


ActiveRecord::Schema.define(version: 2023_01_20_162230) do

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

  create_table "employees", 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.index ["email"], name: "index_employees_on_email", unique: true
    t.index ["reset_password_token"], name: "index_employees_on_reset_password_token", unique: true
  end

  create_table "kudos", force: :cascade do |t|
    t.string "Title", null: false
    t.text "Content", null: false
    t.integer "giver_id", null: false
    t.integer "receiver_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end

`

我试着弄乱括号、大括号和方括号。有人可以修复我的种子文件吗,因为它让我发疯。我知道这是一个菜鸟问题,但在我的学习阶段对我来说仍然是一个严重的问题。

【问题讨论】:

    标签: ruby-on-rails ruby rubygems


    【解决方案1】:

    这是一个示例,说明如何更新种子文件以正确创建具有随机给予者和接受者的随机 Kudo:

    employees = Employee.create!([{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},...])
    
    # randomly select a giver and receiver from the employees array
    giver = employees.sample
    receiver = employees.sample
    
    # create the Kudo with the randomly selected giver and receiver
    kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, giver: giver, receiver: receiver)
    
    

    这将从 employees 数组中随机选择一名员工作为新 Kudo 的提供者和接收者。确保在创建荣誉之前已经创建了员工。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多