【问题标题】:Adding a JSON type to a seeds file in Rails在 Rails 中将 JSON 类型添加到种子文件
【发布时间】:2019-08-27 23:22:55
【问题描述】:
Rails 5.2.3
MySQL 8 server with MySQL gem

我的迁移工作正常:

def change
  add_column :users, :roles, :json
end

记录在用户表中创建为 JSON。我遇到的问题是种子文件。

来自种子文件:

users = [
             {'email':"john@acme.net", 'first_name': "John", 'last_name': "Doe", 'status':'active', 'roles':"['admin']", 'password': "12345678", 'password_confirmation': "12345678"}
    ]
users.each do |user|
  User.create!(user) if User.find_by_email(user[:email]).nil?
end

当我运行 rake 时,出现以下错误:

ActiveModel::RangeError: 1566947904332768 is out of range for ActiveModel::Type::Integer with limit 4 bytes
/db/seeds.rb:9:in `block in <main>'
/db/seeds.rb:8:in `each'
/db/seeds.rb:8:in `<main>'
/bin/rails:9:in `<top (required)>'
/bin/spring:15:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

Process finished with exit code 1

我也试过了:

'roles':['admin']

同样的错误。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5.2 mysql-8.0


    【解决方案1】:

    您的某些列有问题,或者密码或密码确认需要升级到限制(16)或其他东西才能接受您尝试保存的字符

        change_column :users, :password, :string, :limit => 55
        change_column :users, :password_confirmation, :string, :limit => 55
    

    使用这句话运行迁移,如果可行,现在就让我们

    【讨论】:

    • 没有。除非我出于某种原因需要硬限制,否则默认字符串类型是 256 个字符,并且 password 和 password_confirmation 都是字符串类型。请注意,它们包含在 "" 中,因此它们不被视为整数。
    • 在你的表中哪个属性是整数类型?
    • 我没有整数类型的属性
    • 如果您在此列中没有任何整数类型,唯一剩下的就是 id 列已满,您在此模型上有 id 吗?
    • 问题出在已审核的 gem 上。它期望 user_id 是一个整数,并从 user_id 中获取前几个字符,并尝试将其转换为整数。参考:stackoverflow.com/questions/57700909/…
    猜你喜欢
    • 2014-12-26
    • 2019-06-22
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    相关资源
    最近更新 更多