续集迁移。我的迁移文件中的IP地址应该是什么?

时间:2015-01-01 20:52:54

标签: database postgresql sequel

所以这是我的续集迁移表。我正在为了学习而创建一些虚拟网络软件,我正在尝试创建一个名为payloads的数据库表,它正在接收并组织通过cURL请求进入的数据。 cURL带有称为有效负载的数据,我正在尝试在迁移过程中组织所有这些数据。

这是我的迁移代码:

Sequel.migration do
  change do
    create_table(:payloads) do
      primary_key   :id
      String        :url
      String        :requestedAt
      String        :respondedIn
      String        :referredBy
      String        :requestType
      String        :parameters
      String        :eventName
      String        :userAgent
      Integer       :resolutionWidth
      Integer       :resolutionHeight
      Integer       :ip
    end
  end
end

这是我的cURL请求:

curl -i -d 'payload={"url":"http://jumpstartlab.com/blog","requestedAt":"2013-02-16 21:38:28 -0700","respondedIn":37,"referredBy":"http://jumpstartlab.com","requestType":"GET","parameters":[],"eventName": "socialLogin","userAgent":"Mozilla/5.0 (Macintosh%3B Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17","resolutionWidth":"1920","resolutionHeight":"1280","ip":"63.29.38.211"}' http://localhost:9393/sources/jumpstartlab/data

curl请求命中一个post路由(curl请求有-d,这显然意味着它需要命中一个post路由),它有一个create方法。这是方法。

  def self.create(attributes)
      #might need to turn symbols into strings
      binding.pry
      table.insert(
      :url => attributes[:url],
      :requestedAt => attributes[:requestedAt],
      :respondedIn => attributes[:respondedIn],
      :referredBy => attributes[:referredBy],
      :requestType => attributes[:requestType],
      :parameters => attributes[:parameters],
      :eventName => attributes[:eventName],
      :userAgent => attributes[:userAgent],
      :resolutionWidth => attributes[:resolutionWidth],
      :resolutionHeight => attributes[:resolutionHeight],
      :ip => attributes[:ip]
      )
    end

在上面的代码片段中,table是一个方法:

def self.table
   DB.from(:payloads)
end

这是我的错误消息:

Sequel::DatabaseError - PG::Error: ERROR:  invalid input syntax for integer: "63.29.38.211"
LINE 1: ...hrome/24.0.1309.0 Safari/537.17', '1920', '1280', '63.29.38....
                                                            ^

任何想法是怎么回事?我的想法是我的迁移表没有正确处理通过有效负载进入的IP字段。有效载荷进入,并且params散列可能将所有内容转换为字符串。当数据到达我的迁移表时,我试图将其中一些字段转换为整数,但我的迁移表无法将IP地址字符串转换为整数。那么我的迁移表字段应该用于IP列?它应该是Float吗?我可以使用字符串,但是如果我不想将它作为字符串读取呢?

1 个答案:

答案 0 :(得分:1)

您需要pg_inet extension

  

pg_inet扩展增加了对Sequel的支持,以处理PostgreSQL的问题   使用ruby的IPAddr类的inet和cidr类型。