【发布时间】:2014-02-16 05:10:11
【问题描述】:
这必须有一个我想念的明显答案!
schema.rb 有
create_table "table_name", :force => true do |t|
t.integer my_columnname, :limit =>8
seeds.rb 有
TableName.create([{"my_columnname"=>-1}])
但是在查看数据库时,该值为 0,例如零。它没有正确填充。
注释:
- 如果我将 -1 更改为 999,则数据库值为 999。所以我知道 seed.rb 格式是可以的
- 我尝试使用 :my_columname 而不是“my_columnname”,没有区别
- 我已经搜索过任何有关播种负值问题的示例,但似乎没有人遇到同样的问题
- 过去我确实有这个工作,所以我区分了种子、模式和迁移文件以及 application.rb。唯一的区别似乎是 :limit => 8 (我需要大整数)。但我研究的所有内容都表明 BIGINT 已签名。所以“不应该”是问题所在。
- 我已经登录 mysql 查看表类型 - 它是 bigint
- 我已经从当前版本中删除了 :limit,但它仍然没有填充
- 我已登录 mysql 命令行并执行了一个带有负值的 sql 插入并填充。
- rake db:seed --trace 没有显示任何错误。
** 更新 **
- 尝试插入值 9999999999999999999 会导致错误:Mysql2::Error: Out of range value for column 'qty_included_standard'
- 我尝试将值设置为“-1”或-1。没有区别仍然不会设置为-1
-
使用控制台,提交给数据库的实际值是零而不是 -1(参见下面的 value_included_standard)。但如果我制作 -1 888,它会提交 888。
014-02-17 20:55:02", special_consideration: nil>] 1.9.3-head :009 > TransactionType.create([{"transaction_type"=>"20", "value_included_standard"=>-1, "qty_included_standard"=>"8", "cost_per_transaction_standard"=>"1000", " standard_included_in_overall"=>"false", "special_consideration"=>nil, :plan_id=>1}]) (0.3ms) 开始 SQL (0.4ms) INSERT INTO
transaction_types(cost_per_transaction_standard,cost_per_transaction_within,created_at,flag_fall_cost_for_transaction,min_block_for_transaction,plan_id,qty_included_standard,standard_included_in_overall, @987653332@, @98765434 ,transaction_type,updated_at,value_included_standard,value_included_within,withintype_included_in_overall) 值 (1000, 0, '2014-02-17 20:55:43', 0, 0, 1, 8, 0, NULL, 0, '20', '2014-02-17 20:55:43', 0, 0, NULL)
我做错了什么。迁移/模式中是否存在限制签名值的内容?
希望这对某人来说是显而易见的。
** Invoke db:seed (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
WE HAVE INITIALISED
** Execute db:abort_if_pending_migrations
** Execute db:seed
【问题讨论】:
-
据我所知,
create采用散列,而不是包含散列的列表。让它TableName.create( {:my_columnname => -1} )。同样,要使此功能起作用,my_columname必须使用attr_accessible启用批量分配。该命令位于种子文件中这一事实并不意味着任何事情。当您从 rails 控制台执行此操作时会发生什么?运行命令然后TableName.all看看发生了什么。 -
对不起,我查了
create,该数组不应该引起问题,尽管它也没有任何作用并且可以删除。事实上,花括号也是可选的。 -
您使用的是什么版本的 Rails 和 Ruby?我在 Ruby 2.1 上使用 Rails 4.0 尝试了这个,负数被播种得很好。唯一不同的是我使用了 sqlite3。
-
感谢您的见解。稍后将尝试控制台。 Rails 3.0 Attr 很有趣。但正如我所提到的,如果我将值设为 888,它确实有效。更新!
-
您对该模型有什么有趣的验证或 before_* 过滤器吗?该模型是否会限制该字段的负数?
标签: ruby-on-rails ruby-on-rails-3 activerecord rails-activerecord