# Trouble with IDs

**URL:** <https://www.ruby-forum.com/t/trouble-with-ids/108044>\
**Category:** Rails\
**Created:** [June 28, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/trouble-with-ids/108044 "2007-06-28T17:19:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [June 28, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/trouble-with-ids/108044/1 "2007-06-28T17:19:48Z")

</div>

Hello,

I am getting strange results with the following code:

def self.process\_product(supplier\_productid)  
local\_product = Product.find(:first, :conditions =\> [“supplierid =  
9 and supplier\_productid = :id”, {:id =\> supplier\_productid}])

```
if local_product
  local_product.update_attributes(attributes)

  begin
    supplier_stock = SupplierStock.find(local_product.productid)
  rescue Exception => e
    puts e.to_s
    supplier_stock = SupplierStock.new(
      :id => local_product.productid,
      :yesterday_stock => stock || -999,
      :current_stock => stock || -999
    )

```

### supplier\_stock[:productid] = local\_product.productid

```
    supplier_stock.save
  else
    supplier_stock.yesterday_stock = supplier_stock.current_stock
    supplier_stock.current_stock = stock
    supplier_stock.save
  end
else
  ...

```

The database information:  
###################################

db=# \d supplier\_stock  
Table “public.supplier\_stock”  
Column | Type | Modifiers  
-----------------±--------±-------------------  
productid | integer | not null  
current\_stock | integer | not null default 0  
yesterday\_stock | integer | not null default 0  
Indexes:  
“supplier\_stock\_pkey” primary key, btree (productid)  
Foreign-key constraints:  
“supplier\_stock\_productid\_fkey” FOREIGN KEY (productid) REFERENCES  
supplier\_products(productid) ON DELETE CASCADE

db=# select \* from supplier\_products\_productid\_seq;  
sequence\_name | last\_value | increment\_by |  
max\_value | min\_value | cache\_value | log\_cnt | is\_cycled |  
is\_called  
---------------------------------±-----------±-------------  
±--------------------±----------±------------±--------±----------  
±----------  
supplier\_products\_productid\_seq | 464931 | 1 |  
9223372036854775807 | 1 | 1 | 32 | f |  
t  
(1 row)

###################################

And the error (occurs on the first call to save, in the exception  
handling code):

RuntimeError: ERROR C23502 Mnull value in column “productid”  
violates not-null constraint FexecMain.c L1782  
RExecConstraints: INSERT INTO supplier\_stock (“yesterday\_stock”,  
“current\_stock”) VALUES(-999, -999)

> From my understanding of Rails, when I insert to the supplier\_stock  
> table, the primary key (defined as productid in the database table and  
> the class both) should be determined automatically whether I  
> define :id in the SupplierStock object’s new method or not. You will  
> notice I have commented one line of code. Uncommenting that line  
> eliminates the error. Why is it when I explicitly specify :productid  
> outside the new method that it works? Additionally, when I create the  
> SupplierStock object, if I immediately print out the keys and values  
> after the object is created, :id is not defined at all. Any insights?  
> Thanks.
