I am using golang and postgreSQL version 9.5.5 in my application. I am using "github.com/lib/pq" as my database driver to connect to the database. One of my fields have the type timestamp with timezone. I would like to update to the current time. So I used the following code:
Note:I m using beego as my framework and use orm to compute my
queries.
_, err := o.Raw("UPDATE leave SET resultdate=? WHERE leaveid=?", time.Now(), leaveResult.LeaveId).Exec()
When I execute this I'm getting the following error:
"pq: invalid input syntax for type timestamp with time zone: \"09:24:29\""
Appreciate any help.
High probability that DB expects a different date/time format. For example RFC3339. Try saving instead of time.Now() with time.Now().Format(time.RFC3339)
Related
I'm using go-gorm with a postgres 11 DB and facing an issue where I need to remove the RETURNING clause entirely when creating records (that statement seems to be included by default). I just want to insert records and get nothing back, except for errors.
I have some complex relations on the database that won't support RETURNING statements, so when I try to insert like this: (made code simpler for brevity)
type Cargo struct {
Id int64 `gorm:"primaryKey"`
Name string
}
dsnString := fmt.Sprintf("host=%s ...")
db, _ := gorm.Open(postgres.New(postgres.Config{DSN: dsnString}), &gorm.Config{})
cargo := Cargo{Name: "test"}
db.Create(cargo)
I get the error "ERROR: cannot perform INSERT RETURNING on relation Cargo".
I tried creating the db connection with the parameter WithoutReturning: true:
db, _ := gorm.Open(postgres.New(postgres.Config{DSN: dsnString, , WithoutReturning: true}), &gorm.Config{})
But then when I try db.Create(cargo) I get a different error: "LastInsertId is not supported by this driver". It seems to be still trying to get the last inserted id anyway.
In go-pg I could use db.Model(x).Returning("null").Insert(cargo) but I couldn't find a way to do it with go-gorm. Any help is greatly appreciated.
The only two ways that I can get gorm to not use the RETURNING clause with postgres are
A model that does not declare a primary key
That means getting rid of the field that is named ID/Id or any tagged gorm:"primaryKey".
type Cargo struct {
Name string
}
db.Create(&Cargo{Name: "Test"})
Using Create from map with Table()
In this case you would represent your model as a map[string]interface{} instead of as a struct and use it like this:
db.Table("cargos").Create(map[string]interface{}{
"name": "Test",
})
As it stands gorm doesn't support this use case very well. If you can't restructure your views to support RETURNING and these options aren't doing it for you, I suggest adding a feature request in the gorm repo.
You can modify the default returning behavior with
db.Clauses(clause.Returning{}).Create(&cargo)
Here the doc link: https://gorm.io/docs/update.html#Returning-Data-From-Modified-Rows
In my JMeter test case, I'm selecting a timestamp with timezone field from a postgresql DB.
The thing is, when I run the test case on a fresh instance of JMeter for the first time, the value is converted to my local datetime.
Value in DB: 2019-10-23 06:20:54.086605+00
Value when using select: 2019-10-23 11:50:54.086605
But often when I run the test case again on the same JMeter instance, it is not converted.
Value in DB: 2019-10-23 06:42:15.77647+00
Value when using select: 2019-10-23 06:42:15.77647
Restarting JMeter will again result in the 1st behavior. I'm not able to exactly pinpoint how and when this switch in behavior happens. It happens now and then, but restarting JMeter will always reset to the 1st behavior.
I have tried setting the timezone value in postgres.conf file as well as user.timezone value in system.properties in JMeter /bin directory to UTC, to no avail.
I'm using SELECT * to select all columns from the table and storing them in variables using the Variable names field in JDBC Request.
The reason is that PostgreSQL timestamp with time zone is being mapped to java.sql.Timestamp which doesn't contain timezone information.
The only workaround I can think of is converting the aforementioned Timestamp providing the TimeZone as the parameter like:
In the JDBC Request sampler define Result Variable Name
Add JSR223 PostProcessor as a child of the request and use the below Groovy code to convert the Timestamp into the TimeZone of your choice:
vars.getObject("resultSet").get(0).get("bar").format('yyyy-MM-dd HH:mm:ss.SSSSSSZ', TimeZone.getTimeZone('UTC'))
More information: Debugging JDBC Sampler Results in JMeter
I am currently learning MongoDB and while trying to explore the documentation I am encountering some anomaly while trying to use the ObjectId.getTimestamp() function. I executed the below is the lines of code at my machine's time 20:12. in mongo shell
>x= new ObjectId()
>ObjectId("5ac4e4362b7738556969dbba")
>x.getTimestamp()
>ISODate("2018-04-04T14:41:58Z")
I want to know why is there a different timestamp value being shown and not the exact timestamp when the Id was created ? Is it due to some UTC setting which should be done in a Mongo configuration file ?
I want to store timestamp with time zone in dashdb. I see that db2 supports data type TIMESTAMP WITH TIME ZONE for this purpose. But when I am trying to create table in dash db using this type, I am getting below error. Is this because dash db just doesn't support this type, even though db2 does. Or is there something I am doing wrong? Appreciate the help
DDL failed with message
_ Exception. _ state = 42601; error code = -104; error Message = Error for >batch element #1: An unexpected token "time" was found following "COL2" >timestamp with". Expected tokens may include: "REFERENCES".. _CODE=-104, >_STATE=42601, DRIVER=3.66.46
I have a field in my postgres database using the time (without time zone) data type. I have a Microsoft Access front-end for the database connected using psqlODBC, which reads this field as a "Date/Time" data type.
If I try to insert something into the field through the front end, I get the following error:
ODBC - insert on a linked table "table_name" failed.
ERROR: column "column_name" is of type time without time zone but expression is of type date;
I'm assuming that access is trying to input a time stamp instead.
Basically my question is it even really possible to use the time data type with Access? Or should I just be using the timestamp datatype instead?
If you are manually typing data into a linked table then no this won't be possible at present, if you have the option of updating your table via forms or VB then you could try this to get access to produce only a time value:
TimeSerial(Hour(Now()), Minute(Now()), Second(Now()))
Otherwise as you say, it's probably a good idea to change your data type to timestamp.