# Timestamps casted to nil?

**URL:** <https://www.ruby-forum.com/t/timestamps-casted-to-nil/55152>\
**Category:** Rails\
**Created:** [March 17, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/timestamps-casted-to-nil/55152 "2006-03-17T13:10:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 17, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/timestamps-casted-to-nil/55152/1 "2006-03-17T13:10:16Z")

</div>

Hi,

for some reason, all timestamp fields with or without time zone in my  
Postgres tables seem to be casted to nil. From console:

> > me = User.find(‘PS12345’)  
> > =\> #\<User:0x2379788 @attributes={“mtime”=\>“01.03.2006 13:26:32.737166”,  
> > “valid”=\>“8”, “valid\_from”=\>“01.03.1999 14:09:21 CET”,  
> > “valid\_thru”=\>nil, “lastlogin”=\>“13.03.2006 16:04:58”,  
> > “password”=\>“cbd8f7984c654c25512e3d9241ae569f”, “login”=\>“PS12345”,  
> > “email”=\>“removed\_email\_address@domain.invalid”}\>
> 
> > puts me.valid\_from  
> > nil  
> > =\> nil

Using before\_type\_cast, I can see the content is still there, albeit as  
string:

> > puts me.valid\_from\_before\_type\_cast  
> > 01.03.1999 14:09:21 CET  
> > =\> nil

Saving a modified object sets all timestamps to nil:

> > me.valid=0  
> > =\> 0
> 
> > me.save  
> > ActiveRecord::StatementInvalid: PGError: ERROR: null value in column  
> > “valid\_from” violates not-null constraint  
> > : UPDATE users SET “email” = ‘removed\_email\_address@domain.invalid’, “mtime” = NULL,  
> > “lastlogin” = NULL, “valid\_from” = NULL, “valid” = 0, “password” =  
> > ‘cbd8f7984c654c25512e3d9241ae569f’, “valid\_thru” = NULL WHERE login =  
> > ‘PS12345’

This happens for me with Ruby 1.8.2 and 1.8.4, Rails 1.0.0 on OS X and  
Windows, with postgres-pr or ruby-postgres as gems (Postgres 8.1.2). I’d  
be thankful for any ideas on why this happens …

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 17, 2006, 1:48pm UTC](https://www.ruby-forum.com/t/timestamps-casted-to-nil/55152/2 "2006-03-17T13:48:41Z")

</div>

I think the problem is date format returned from server.  
The odds are your server parameter DateStyle is set to German, so it  
can’t be directly parsed using Date.parse or Time.parse.  
While DateStyle is set to “ISO, MDY” everything should work fine.  
You should either adjust ‘datestyle’ parameter in postgresql.conf and  
restart PG server or call  
ActiveRecord::Base.connection.execute(“SET DateStyle TO ISO, MDY”)

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 17, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/timestamps-casted-to-nil/55152/3 "2006-03-17T14:30:19Z")

</div>

After a quick check on the console, this seems to to have been the  
cause. Since I cannot modify the server configuration itself, setting it  
per-connection with connection.execute is the way to go for me.

Many thanks for your help, Nikolai!

- Jan

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 17, 2006, 1:57pm UTC](https://www.ruby-forum.com/t/timestamps-casted-to-nil/55152/4 "2006-03-17T13:57:51Z")

</div>

Sorry, I’m a bit wrong.  
You can also do it w/o restarting server:  
ALTER DATABASE your\_db\_name SET DateStyle TO ISO;

> **[20.11. Client Connection Defaults](https://www.postgresql.org/docs/8.1/runtime-config-client.html)**
>
> 20.11. Client Connection Defaults 20.11.1. Statement Behavior 20.11.2. Locale and Formatting 20.11.3. Shared Library Preloading 20.11.4. Other Defaults 20.11.1. Statement Behavior client\_min\_messages …
