skip to Main Content

I am pretty new to golang and gorm so, probably, this is an old question:
I have a table in postgresql named datetime with type timestamp

I am trying to retrieve it using gorm in a golang project with this mapping

  DateTime time.Time `gorm:"datetime:timestamp"`

But when i run the code i see that it retrieves this value:

0001-01-01 00:00:00 +0000

How can i solve this ?

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution and it was pretty simple to be honest. The field was mapped as

    DateTime
    

    which i guess is translated as "date_time" when performing the query to extract the data; so, i changed the mapping to

    Datetime
    

    which is translated to "datetime", and it worked


  2. DateTime time.Time gorm:"datetime:timestamp;default:CURRENT_TIMESTAMP"
    or
    DateTime time.Time gorm:"datetime:timestamp;default:DEFAULT"
    try this. This will store current time. In your case you are not storing any data that’s why it retrieves 0001-01-01 00:00:00 +0000

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search