# Not null error from PG on build\_other call

**URL:** <https://www.ruby-forum.com/t/not-null-error-from-pg-on-build-other-call/185368>\
**Category:** Rails\
**Created:** [March 18, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/not-null-error-from-pg-on-build-other-call/185368 "2010-03-18T21:25:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 18, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/not-null-error-from-pg-on-build-other-call/185368/1 "2010-03-18T21:25:45Z")

</div>

I have this code in a stand alone script that uses nokogiri with  
activerecord to parse an xml file and initialise a database.

```
  begin
  this_client = correspondent_f.build_client
  rescue => e
    puts("Rescued error in build.")
    puts(e.backtrace)
    puts(" ")
    puts(correspondent_f.inspect.to_yaml)
    raise e
  end

```

Which, for a certain input record generates this error:

Rescued error in build.  
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/  
connection\_adapters/abstract\_adapter.rb:219:in `log' /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/ connection_adapters/postgresql_adapter.rb:550:in`execute’  
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/  
connection\_adapters/abstract/database\_statements.rb:265:in  
`update_sql' /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/ connection_adapters/postgresql_adapter.rb:561:in`update\_sql’  
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/  
connection\_adapters/abstract/database\_statements.rb:49:in  
`update_without_query_dirty' /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/ connection_adapters/abstract/query_cache.rb:18:in`update’  
. . .  
— “#\<Correspondent id: 1, correspondent\_common\_name: “tnt skypak  
international exp.”, correspondent\_legal\_name: “TNT SKYPAK  
INTERNATIONAL EXP.”, correspondent\_legal\_form: “UNKN”,  
correspondent\_legal\_name\_key: “tntskypakinternationalexp”,  
correspondent\_status: “HOLD”, changed\_at: “2010-03-18 19:49:49”,  
changed\_by: “hp3000\_client\_import”, created\_at: “2010-03-18  
19:49:49”, created\_by: “hp3000\_client\_import”, lock\_version: 0\>”  
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record

/connection\_adapters/abstract\_adapter.rb:219:in `log': PGError: ERROR: null value in column "correspondent_id" violates not-null constraint (ActiveRecord::StatementInvalid) : UPDATE "clients" SET "changed_at" = '2010-03-18 20:07:44', "correspondent_id" = NULL, "lock_version" = 1 WHERE id = 1 AND "lock_version" = 0 from /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/ active_record/connection_adapters/postgresql_adapter.rb:550:in`execute’  
from /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/  
active\_record/connection\_adapters/abstract/database\_statements.rb:  
265:in `update\_sql’  
. . .

Now, I know that the record triggering the exception is a duplicate of  
one already previously loaded into the clients table. This in fact  
should trigger a validation error in the Correspondent class. But I do  
not see that validation error anywhere and I cannot understand why the  
correspondent\_id value is null in the second case.

The Client class has this:

belongs\_to :correspondent  
validates\_associated  
validates\_presence\_of :effective\_from  
validate :date\_range

The Correspondent class has this:

has\_one :client

I am sure that this situation has a simple explanation and I hope that  
someone can provide it to me.
