Code Monkey home page Code Monkey logo

Comments (16)

jinzhu avatar jinzhu commented on May 6, 2024

Could you post the outputs of below codes in your local?

package main

import "fmt"
import "time"

func main() {
    var t time.Time
    fmt.Println(t)
}

I guess it is your local problem.

from gorm.

jamesharr avatar jamesharr commented on May 6, 2024
0001-01-01 00:00:00 +0000 UTC

I think you might be right, though. Looks like the memory is not being zeroed. I'll try with a different distribution of Go (I was encountering this error with 1.1.x https://code.google.com/p/go/issues/detail?id=6515)

Most of the timezones actually make it through OK

gorm=# select created_at, updated_at, deleted_at from users;
          created_at           |          updated_at           |           deleted_at
-------------------------------+-------------------------------+---------------------------------
 2013-11-23 22:57:58.590727-06 | 2013-11-23 22:57:58.590729-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.595065-06 | 2013-11-23 22:57:58.595067-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.593766-06 | 2013-11-23 22:57:58.595854-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.599053-06 | 2013-11-23 22:57:58.599075-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.598174-06 | 2013-11-23 22:57:58.598175-06 | 2013-11-23 22:57:58.599812-06
 2013-11-23 22:57:58.601707-06 | 2013-11-23 22:57:58.601708-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.629403-06 | 2013-11-23 22:57:58.63039-06  | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.686826-06 | 2013-11-23 22:57:58.686827-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.690354-06 | 2013-11-23 22:57:58.690355-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.692893-06 | 2013-11-23 22:57:58.692895-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.695043-06 | 2013-11-23 22:57:58.695045-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.726714-06 | 2013-11-23 22:57:58.726716-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.782825-06 | 2013-11-23 22:57:58.782826-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.586562-06 | 2013-11-23 22:57:58.586569-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.588104-06 | 2013-11-23 22:57:58.588105-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.588966-06 | 2013-11-23 22:57:58.588968-06 | 0001-12-31 18:09:24-05:50:36 BC
 2013-11-23 22:57:58.589875-06 | 2013-11-23 22:57:58.589876-06 | 0001-12-31 18:09:24-05:50:36 BC
(17 rows)

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

Ok, Thank you. Let me know if there are any updates.

I tested gorm with go1.2rc5 (linux gcc 4.8.2) (installed with gvm https://github.com/moovweb/gvm), and all tests passed.

So I am going to close the issue first. feel free to reopen it if you still have issue.

from gorm.

jamesharr avatar jamesharr commented on May 6, 2024

Tested with Go 1.1.2 on Ubuntu linux/amd64 and have the same issue. The problem might be with lib/pq.

gorm_test logs:

[2013-11-24 22:58:52]  [1.58ms]  INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ('2000-10-27 12:02:40 +0000 UTC','18','
1','2013-11-24 22:58:52.848919419 -0600 CST','0001-01-01 00:00:00 +0000 UTC','0','2013-11-24 22:58:52.84891059 -0600 CST','{0 false}','[]') RETURNING "id"

[2013-11-24 22:58:52]  [1.17ms]  INSERT INTO users (created_at,billing_address_id,password_hash,birthday,age,name,updated_at,deleted_at,shipping_address_id) VALUES ('2013-11-24 22:58:52.850864691 -0600 C
ST','{0 false}','[]','2002-01-01 00:00:00 +0000 UTC','20','2','2013-11-24 22:58:52.850869987 -0600 CST','0001-01-01 00:00:00 +0000 UTC','0') RETURNING "id"

[2013-11-24 22:58:52]  [1.62ms]  INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ('2005-01-01 00:00:00 +0000 UTC','22','
3','2013-11-24 22:58:52.852519176 -0600 CST','0001-01-01 00:00:00 +0000 UTC','0','2013-11-24 22:58:52.852514785 -0600 CST','{0 false}','[]') RETURNING "id"

[2013-11-24 22:58:52]  [0.98ms]  INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ('2010-01-01 00:00:00 +0000 UTC','24','
3','2013-11-24 22:58:52.854489327 -0600 CST','0001-01-01 00:00:00 +0000 UTC','0','2013-11-24 22:58:52.854485646 -0600 CST','{0 false}','[]') RETURNING "id"

[2013-11-24 22:58:52]  [0.95ms]  INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ('2010-01-01 00:00:00 +0000 UTC','26','
5','2013-11-24 22:58:52.855866734 -0600 CST','0001-01-01 00:00:00 +0000 UTC','0','2013-11-24 22:58:52.855863206 -0600 CST','{0 false}','[]') RETURNING "id"

Postgres logs

2013-11-24 22:58:52 CST LOG:  execute <unnamed>: INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING "id"
2013-11-24 22:58:52 CST DETAIL:  parameters: $1 = '2000-10-27 07:02:40-05', $2 = '18', $3 = '1', $4 = '2013-11-24 22:58:52.848919-06', $5 = '0001-12-31 18:09:24-05:50:36 BC', $6 = '0', $7 = '2013-11-24 22:58:52.848911-06', $8 = NULL, $9 = '\x'
2013-11-24 22:58:52 CST LOG:  execute <unnamed>: INSERT INTO users (created_at,billing_address_id,password_hash,birthday,age,name,updated_at,deleted_at,shipping_address_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING "id"
2013-11-24 22:58:52 CST DETAIL:  parameters: $1 = '2013-11-24 22:58:52.850865-06', $2 = NULL, $3 = '\x', $4 = '2001-12-31 18:00:00-06', $5 = '20', $6 = '2', $7 = '2013-11-24 22:58:52.85087-06', $8 = '0001-12-31 18:09:24-05:50:36 BC', $9 = '0'
2013-11-24 22:58:52 CST LOG:  execute <unnamed>: INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING "id"
2013-11-24 22:58:52 CST DETAIL:  parameters: $1 = '2004-12-31 18:00:00-06', $2 = '22', $3 = '3', $4 = '2013-11-24 22:58:52.852519-06', $5 = '0001-12-31 18:09:24-05:50:36 BC', $6 = '0', $7 = '2013-11-24 22:58:52.852515-06', $8 = NULL, $9 = '\x'
2013-11-24 22:58:52 CST LOG:  execute <unnamed>: INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING "id"
2013-11-24 22:58:52 CST DETAIL:  parameters: $1 = '2009-12-31 18:00:00-06', $2 = '24', $3 = '3', $4 = '2013-11-24 22:58:52.854489-06', $5 = '0001-12-31 18:09:24-05:50:36 BC', $6 = '0', $7 = '2013-11-24 22:58:52.854486-06', $8 = NULL, $9 = '\x'
2013-11-24 22:58:52 CST LOG:  execute <unnamed>: INSERT INTO users (birthday,age,name,updated_at,deleted_at,shipping_address_id,created_at,billing_address_id,password_hash) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING "id"
2013-11-24 22:58:52 CST DETAIL:  parameters: $1 = '2009-12-31 18:00:00-06', $2 = '26', $3 = '5', $4 = '2013-11-24 22:58:52.855867-06', $5 = '0001-12-31 18:09:24-05:50:36 BC', $6 = '0', $7 = '2013-11-24 22:58:52.855863-06', $8 = NULL, $9 = '\x'

I'm going to see if I can get closer to isolating the bug.

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

Thank you for your update, hope you could point it out soon.

from gorm.

jamesharr avatar jamesharr commented on May 6, 2024

Yup, it's in lib/pq. I can reproduce it with this.

import (
        _ "github.com/lib/pq"
        "database/sql"
        "time"
        "fmt"
)

func main() {
        db, _ := sql.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
        sql := `INSERT INTO users (deleted_at) VALUES ($1) RETURNING "id"`
        res, err := db.Exec(sql,time.Time{})
        fmt.Println(res, err)
}

Postgres logs

LOG:  execute <unnamed>: INSERT INTO users (deleted_at) VALUES ($1) RETURNING "id"
DETAIL:  parameters: $1 = '0001-12-31 18:09:24-05:50:36 BC'

What version of Postgres and lib/pq do you use? It seems to always treat the timezone as "-05:50:36" for me. Even several revisions back.

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

I am using latest lib/pq 9fa22d47b017aa81fcfd425900e2c4c48a8170a7 and postgres 9.3.1.

from gorm.

bendemaree avatar bendemaree commented on May 6, 2024

I have a (nearly) identical issue using the latest gorm, pq, etc. This is Go 1.3.3 on darwin_amd64. I ran the small test file and also received 0001-01-01 00:00:00 +0000 UTC.

The query looks like this, from the gorm logger:

UPDATE "users" SET "updated_at" = '2014-10-02 19:34:37.443809139 -0500 CDT', "deleted_at" = '-0001-12-31 18:09:24 -0550 -0550', "first_name" = '', "password" = 'mypass', "token" = 'mytoken', "created_at" = '2014-10-02 19:19:39.436759 -0500 CDT', "email" = '', "username" = 'myusername'  WHERE (deleted_at IS NULL OR deleted_at <= '0001-01-02') AND ("id" = '1')

You'll notice that the deleted_at field is incorrect and shows -0500 -0500 at the end of its timestamp.

The actual error:

pq: invalid input syntax for type timestamp with time zone: "-0001-12-31T18:09:24-05:50:36"

I added some debugging statements as deep as right after this line that show that even at this point the same -0500 -0500 string is there (I just printed out field.Field.Interface()).

Any ideas? This only happens for this DeletedAt field for this model; all others are working, despite having identical patterns.

Did you have any luck @jamesharr? I'd appreciate any help you or @jinzhu can provide.

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

Hi @bendemaree

As I remembered, I have seen similar reports before... but forgot how they fixed it

Could you provide a script that could reproduce this issue after pull latests lib/pg, so I could test it myself?

from gorm.

bendemaree avatar bendemaree commented on May 6, 2024

Sure! I will have something soon that (hopefully) reproduces.

from gorm.

bendemaree avatar bendemaree commented on May 6, 2024

Hey @jinzhu, I have created a test that fails every time on my machine.

package main

import (
        "fmt"
        "github.com/jinzhu/gorm"
        _ "github.com/lib/pq"
        "time"
)

type User struct {
        Id        int64
        Name      string
        Email     string
        CreatedAt time.Time
        UpdatedAt time.Time
        DeletedAt time.Time
}

func main() {
        db, _ := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")

        db.DropTableIfExists(&User{})
        db.CreateTable(&User{})
        db.AutoMigrate(&User{})

        db.LogMode(true)

        user := User{
                Name:  "Test User",
                Email: "[email protected]",
        }

        db.Save(&user)

        dbuser := User{}
        db.First(&dbuser, user.Id)

        dbuser.Email = "[email protected]"

        db.Save(&dbuser)

        testuser := User{}
        db.First(&testuser, dbuser.Id)

        fmt.Println(testuser.Email)
}

This fails for me every time with

[2014-10-04 08:26:00]  pq: invalid input syntax for type timestamp with time zone: "-0001-12-31T18:09:24-05:50:36"

If you create a Postgres database named gorm and have a user named gorm you should be able to just go run this file.

Thank you very much for investigating!

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

selection_389

I haven't got any problem... could you try to update gorm and pg, then test it again?

go get -u github.com/lib/pq 
go get -u github.com/jinzhu/gorm 

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

Hi @bendemaree

I see, seems it is caused by your time zone. and look back @jamesharr's comment

Yup, it's in lib/pq. I can reproduce it with this.

import (
        _ "github.com/lib/pq"
        "database/sql"
        "time"
        "fmt"
)

func main() {
        db, _ := sql.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
        sql := `INSERT INTO users (deleted_at) VALUES ($1) RETURNING "id"`
        res, err := db.Exec(sql,time.Time{})
        fmt.Println(res, err)
}

Seems it is lib/pg's issue, maybe you need to open an issue there?

from gorm.

bendemaree avatar bendemaree commented on May 6, 2024

Actually @jinzhu I think you have the same issue, regardless of timezone. If you look at the deleted_at field in the third line of DB logging, you'll see +0805 +0805 where I think it should be +0800 CST. I don't know why Postgres didn't complain for you.

I have verified @jamesharr's comment as well. I also see -05:50:36 BC. I will open an issue with lib/pq. Thank you!

from gorm.

bendemaree avatar bendemaree commented on May 6, 2024

Sorry. I should have looked more closely at open issues. lib/pq#251 is open already.

from gorm.

bendemaree avatar bendemaree commented on May 6, 2024

@jinzhu If you have time, would you review my comments in lib/pq#253? I think that's the issue. I will try and make a pull request soon.

from gorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.