Mysql rails将错误的值保存到数据库
我不确定,但我修改了此代码以尝试将值777放入数据库它总是将此旧序列号2147483647值存储在数据库中(即使我使用2147483647序列号删除了该记录),它仍然会将2147483647而不是777插入数据库中的记录:Mysql rails将错误的值保存到数据库,mysql,ruby-on-rails,Mysql,Ruby On Rails,我不确定,但我修改了此代码以尝试将值777放入数据库它总是将此旧序列号2147483647值存储在数据库中(即使我使用2147483647序列号删除了该记录),它仍然会将2147483647而不是777插入数据库中的记录: def create @cart = current_cart quantity = nil resource = nil args = {} args[:quantity] = is_number?(params[:quantity
def create
@cart = current_cart
quantity = nil
resource = nil
args = {}
args[:quantity] = is_number?(params[:quantity]) ? params[:quantity].to_i : 1
if params[:sellable_type] == "Product"
@product = Product.find(params[:sellable_id])
args[:resource] = @product
elsif params[:sellable_type] == "AirtimePlan"
@airtime_plan = AirtimePlan.find(params[:sellable_id])
if @cart.airtime_list_items.any?
flash[:notice] = {:too_many_plans => "Can only purchase one airtime plan at a time"}
render "airtime_plans/show" and return
end
if ['MarineTrac','MotoTrac','FleetTrac','AutoTrac'].include? params[:unit_type]
args[:unit_type] = params[:unit_type]
else
flash[:notice] = {:nonexistant_plan => "No such plan exists"}
render "airtime_plans/show" and return
end
args[:resource] = @airtime_plan
end
args[:serial_number] = params[:serial_number]
@line_item = @cart.add_sellable_item args
#save line item to update its quantity, this will also save association with carton
if @line_item.save
redirect_to cart_path(@cart), notice: 'Line item created.'
else
if params[:sellable_type] == "Product"
render "products/show"
else
flash[:notice] = @line_item.errors.messages
render "airtime_plans/show"
end
end
end
推车模型
我试着使用重新加载,我试着强制创建!我尝试显式设置序列号,但它仍然创建序列号为2147483647的记录。我甚至在行项目模型上验证了约束的唯一性
请检查此注销,这很有意义:
Started POST "/line_items" for 127.0.0.1 at 2013-05-21 14:01:18 -0400
Processing by LineItemsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IOXew7ITmoysfrxHO3CYwYB2b7TLFhVR7JgwhwxqwXg=", "serial_number"=>"102099394", "unit_type"=>"MarineTrac", "sellable_id"=>"118", "sellable_type"=>"AirtimePlan", "commit"=>"Add to Cart"}
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')
Mysql2::Error: Duplicate entry '2147483647' for key 'index_line_items_on_serial_number': INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')
(0.5ms) ROLLBACK
Completed 500 Internal Server Error in 14ms
ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '2147483647' for key 'index_line_items_on_serial_number': INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')):
app/controllers/line_items_controller.rb:8:in `create'
您是否将
表格
字段设置为int
??因为这里看起来像是值溢出是的,它被设置为int。什么是值溢出?有些疯狂的事情正在发生,因为我甚至更改了创建操作,只是为了创建一个具有特定序列号的行项目,我查看了sql日志,它尝试使用77777值进行更新,但最终仍然尝试插入2147483647。将INT
转换为BIG INT
并尝试该操作。请注意,当我清除浏览器缓存时在chrome中,它将正确的值保存到数据库中。但是这种情况以前发生过,希望您的大整数解决方案能够阻止我清除浏览器缓存。@DevalShah您能解释一下使用大整数而不是整数的原理吗?
Started POST "/line_items" for 127.0.0.1 at 2013-05-21 14:01:18 -0400
Processing by LineItemsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IOXew7ITmoysfrxHO3CYwYB2b7TLFhVR7JgwhwxqwXg=", "serial_number"=>"102099394", "unit_type"=>"MarineTrac", "sellable_id"=>"118", "sellable_type"=>"AirtimePlan", "commit"=>"Add to Cart"}
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')
Mysql2::Error: Duplicate entry '2147483647' for key 'index_line_items_on_serial_number': INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')
(0.5ms) ROLLBACK
Completed 500 Internal Server Error in 14ms
ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '2147483647' for key 'index_line_items_on_serial_number': INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')):
app/controllers/line_items_controller.rb:8:in `create'