简体   繁体   English

当我使用Ruby添加记录时,数据库中的主键是32个字节的空格

[英]The primary key in the database is 32 bytes whitespace when I use Ruby to add a record

This is the code I use to add a record to database: 这是我用来向数据库添加记录的代码:

 def vis_attribute(email){
   :v_uid => 'default_visitor',
   :v_name => 'default_visitor',
   :password_expiration=>(Time.now + 60 * 60 * 24),
   :last_used_timestamp=>(Time.now - 60 * 60 * 24),
   :v_login_id => email,
   :v_password => 'MMM',
 }

 class vis < ActiveRecord::Base
  def add
    @vis = create vis_attribute('AAABBB@MAI.COM')
  end
 end

v_uid is the primary key. v_uid是主键。

The field @vis.v_id is 0 after I run that. 运行该字段后, @vis.v_id字段为0

I'm running MySQL version 5.6.22-enterprise-commercial-advanced MySQL Enterprise. 我正在运行MySQL 5.6.22-企业-商业-高级MySQL企业版。

My Gemfile is: 我的Gemfile是:

gem 'activerecord', '~> 3.0'
gem 'mysql', '2.9.1'

the v_id definition in database v_uid | varchar(32) | NO | PRI | NULL | | 数据库v_uid | varchar(32) | NO | PRI | NULL | |的v_id定义v_uid | varchar(32) | NO | PRI | NULL | | v_uid | varchar(32) | NO | PRI | NULL | | Does anyone know the reason for this? 有人知道原因吗?

I think the ActiveRecord 3 do some magic. 我认为ActiveRecord 3做得有些神奇。

I solve the problem as follows. 我解决问题如下。

def add
    @vis = new vis_attribute('AAABBB@MAI.COM')
    @vis.v_uid="default_visitor"
    @vis.save!
  end

no matter you set self.primary_key = :visitor_uid or not, the new method will ignore the primary key. 无论是否设置self.primary_key = :visitor_uidnew方法都将忽略主键。

Anyone knows why the method new in ActiveRecord do like this. 任何人都知道为什么ActiveRecord中的new方法会像这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM