3.2 incompatible with Date type?

167 views
Skip to first unread message

Sebastian Wojtczak

unread,
Jul 18, 2017, 1:55:25 AM7/18/17
to DataStax Ruby Driver for Apache Cassandra User Mailing List
Hi, I'm not sure if I found an error or I'm unaware of using cassandra driver, here's an example:


2.4.1 :010 > c.session.execute("INSERT INTO testing(id, date, key) VALUES (now(), ?, ?);", arguments: [Date.current.to_s, "aaa"])
Cassandra::Errors::InvalidError: Expected 4 byte long for date (10)
 
2.4.1 :011 > c.session.execute("INSERT INTO testing(id, date, key) VALUES (now(), ?, ?);", arguments: [Date.current, "aaa"])
ArgumentError: Unable to guess the type of the argument: Tue, 18 Jul 2017

This example is taken from docs http://datastax.github.io/ruby-driver/features/basics/datatypes/#using-time-date-smallint-and-tinyint


Ruby 2.4.1
cassandra-driver 3.2.0
Cassandra 3.10

Sandeep Tamhankar

unread,
Jul 19, 2017, 2:22:21 PM7/19/17
to DataStax Ruby Driver for Apache Cassandra User Mailing List
Hi there!

The example you refer to doesn't use bind parameters, so it's not quite the right model to follow. When executing simple (string) statements that have bind parameters, the driver infers the datatypes of the columns based on the argument values given. It currently doesn't have any inference logic for Ruby Date objects. However, there's a simple (and arguably better) way to insert rows in your table. Make a prepared statement, and then execute that however many times as needed. The following tweak to your example should work:

p = c.session.prepare("INSERT INTO testing(id, date, key) VALUES (now(), ?, ?)")
c
.session.execute(p, arguments: [Date.current, "aaa"])

Hope that helps.
-Sandeep
Reply all
Reply to author
Forward
0 new messages