Getting TypeDoesNotMatch on Timestamp field when inserting using Play Framework with Postgres - sql

I'm trying to insert data into a table called users.
I'm only passing a value for the name field and this exception pops up.
I'm not even passing any Timestamp in the parameter.
The data still gets inserted into the database even if this happens. Why though?
Here is the error I'm getting: [RuntimeException: TypeDoesNotMatch(Cannot convert 2014-10-21 17:41:41.982: class java.sql.Timestamp to Long for column ColumnName(users.joined,Some(joined)))]
Here's the code:
DB.withConnection { implicit conn =>
val id: Option[Long] =
SQL("insert into pinglet.users (name) VALUES ('jel124')")
.executeInsert()
outString += id.getOrElse("nuffin'")
}
Info
joined is a field of data type timestamp with time zone.
My scala version is 2.11.1
java version is 1.8.0_25
My postgres jdbc driver is 9.3-1102-jdbc41

I guess the pk returned by INSERT is a timestamp, and you ask it to be parsed as Option[Long.

Related

Google Dataflow: how to insert RECORD non-repeated type field to Big Query?

I'm new to Dataflow. I've got a predefined-schema containing a non-repeated RECORD field called "device":
device.configId: STRING
device.version: STRING
Using a ParDo transform, I tried inserting a TableRow with this kind of field, as follows:
TableRow row = new TableRow();
row.put("field1", "val1");
TableRow device = new TableRow();
device.put("configId", "conf1");
device.put("version", "1.2.3");
row.put("device", device);
out.output(row);
I logged the table row, it looks like this:
{field1=val1, device={configId=conf1, version=1.2.3}}
I output it to a standard transform: BigQueryIO.write()
But the latter issues an error:
java.lang.RuntimeException: java.io.IOException:
Insert failed: [{"errors":[{
"debugInfo":"",
"location":"device.configid",
"message":"This field is not a record.",
"reason":"invalid"
}],"index":0}]
Not sure why, but note the location spells "configid" in lowecase - not in camel case as in the original log.
Any ideas on how to insert such an object to BigQuery?
Found out the problem. Apparently, this error message was caused only when the "configId" field was set to null rather than "conf1". To be exact, it was implicitly set to JSONObject.NULL coming from some input object.

Formatting the result of oql in VisualVM

I have the following oql query running on visualvm against a heap dump and would like the creationTime field formatted as a date time field (its stored as Long).
select { id: s.id.toString(), createdAt: new Date(s.creationTime) }
from org.apache.catalina.session.StandardSession s
The above query lists the following output (snipped)
{
id = 1010827848,
createdAt = sun.org.mozilla.javascript.internal.NativeDate#66106135
}
...
So clearly its been "converted" into a Date but does not display it a human readable format. Doing a toString() on the date object just results in the field being displayed as Invalid Date.
Is it possible to format the the Long field as a Date field?
The id field's value also is off when queried using VisualVM. When I query the same heap dump using Eclipse Analyser I see the right value (which is BE27C51E8BF185A2FB3AA9164EC0C647). What could be happening to that?
The output shows that you are creating JavaScript Date object. The correct part of you OQL should be: createdAt: new java.util.Date(s.creationTime)
There is a known problem with a field, which is named id. See Retrieve "id" field values via VisualVM OQL query for more details. As a workaround you can use s["wrapped-object"].getValueOfField("id") instead of s.id.toString()
With above changes your query should be:
select { id: s["wrapped-object"].getValueOfField("id"),
createdAt: new java.util.Date(s.creationTime).toString() }
from org.apache.catalina.session.StandardSession s

String was not recognized as a valid Boolean Error on varchar column

I am getting this error:
String was not recognized as a valid Boolean.Couldn't store <No> in meetsstd Column. Expected type is Boolean
When I am running this query:
SELECT * FROM work_nylustis_2013_q3.nylustis_details WHERE siteid = 'NYLUSTIS-155718' LIMIT 50
From this code:
Adapter.SelectCommand = New NpgsqlCommand(SQL, MLConnect)
Adapter.Fill(subDT) ' This line throws error
The meetsstd field is a varchar(3) and it does store either a 'Yes' or a 'No' value. How is this getting this confused with a boolean - a varchar should not care whether is holds 'Yes', or 'Si', or 'Oui'? And it only happens on 27 records out of the 28,000 in the table.
I usually blame npgsql for this kind of strangeness, but the last entry in the stack trace is: System.Data.DataColumn.set_Item(Int32 record, Object value)
Any clues?
Thanks!
Brad
To check if it is problem with database or with driver you can reduce problem to one row and column using your current environment:
SELECT meetsstd FROM work_nylustis_2013_q3.nylustis_details WHERE sitenum=1
(of course you must change sitenum into primary key)
Then try such query using psql, pgAdmin or some JDBC/ODBC based general editor.
If psql shows such record which raises error with your Npgsql based application then problem is with Npgsql driver or problem is with displaying query results.
If other tools shows such strange errors then problem is with your data.
Have you changed type of meetsstd field? Mayby you try to show it on some grid and this grid used Boolean field which was converted to Yes/No for displaying?

How to set up an insert to a grails created file with next sequence number?

I'm using a JMS queue to read from and insert data into a postgres table created by grails. The problem is obtaining the next sequence value. I thought I had found the solution with the following statement (by putting "DEFAULT" where the ID should go), but it's no longer working. I must have changed something, because I needed to recreate the table. What's the best way to get around this problem?
ps = c.prepareStatement("INSERT INTO xml_test (id, version, xml_text)
VALUES (DEFAULT, 0, ?)");
UPDATE:
In response to the suggested solution, I did the following:
Added this to the the domain:
class XmlTest {
String xmlText
static constraints = {
id generator:'sequence', params:[name:'xmltest_sequence']
}
}
And changed the insert statement to the following:
ps = c.prepareStatement("INSERT INTO xml_test (id, version, xml_text)
VALUES (nextval('xmltest_sequence'), 0, ?)");
However, when I run the statement, I get the following error:
[java] 1 org.postgresql.util.PSQLException: ERROR: relation "xmltest_sequence" does not exist
Any thoughts?
You can get the value of any sequence in PostgreSQL using nextval function, in your case:
INSERT INTO xml_test (id, version, xml_text) VALUES (nextval('sequence_name_for_this_table'), 0, ?);
And in your grails domain class you can choose the sequence name:
static mapping = {
id generator:sequence, params:[name:'sequence_name_for_this_table']
}
Problem solved.
It turns out that when grails creates a table, it doesn't assign a specific sequence generator to it.
Instead, grails uses a single sequence generator for all tables. This is called "hibernate_sequence".
So, to get around the problem, I included the "nextval" for that in my SQL statement:
ps = c.prepareStatement("INSERT INTO xml_test (id, version, text_field) VALUES (nextval('hibernate_sequence'), 0, ?)");

Converting '' to NULL in doctrine and symfony

Im using the symfony framework with mysql.
I have a field in mysql that is generated by doctrine as:
weight: { type: double, notnull: false, default: NULL }
`weight` double(18,2) NULL DEFAULT NULL
The value is entered using a textbox and the generated sql trys to insert '' into this field if no value is given.
This produces the following error:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'weight' at row 1
How would change this value such that a Doctrine_Null gets used instead?
Also how would i be able to retrieve '(unknown)' for display purposes if the field is null?
Thanks
maybe you could try to use a validator on your form, like sfValidatorNumber ?
http://www.symfony-project.org/api/1_4/sfValidatorNumber
This should happen in your Form/Validation class. If you are expecting to have empty string values submitted then you need to convert those to null as part of this process.
As far as retrieving "unknown" for display i would probably do this as a custom getter on the model class.