# Update\_attribute does an insert?

**URL:** <https://www.ruby-forum.com/t/update-attribute-does-an-insert/109152>\
**Category:** Rails\
**Created:** [July 11, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/update-attribute-does-an-insert/109152 "2007-07-11T22:59:38Z")\
**Posts on this page:** 3\
**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:** [July 11, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/update-attribute-does-an-insert/109152/1 "2007-07-11T22:59:38Z")

</div>

Hello,

I am trying to update an integer value on a table using Rails, but it  
complains that I am violating a not-null constraint because I didn’t  
specify other attributes. My code is as follows:

```
sup = Supplier.new
sup.update_attribute('num_products', total_products)

```

The error I get is:

RuntimeError: ERROR C23502 Mnull value in column “name” violates  
not-null constraint FexecMain.c 1782 RExecConstraints:  
INSERT INTO suppliers(“name”, “num\_products”, “drop\_ship\_fee”,  
“real\_name”, “return\_policy”, shipping\_terms", “url”, “description”)  
VALUES(NULL, 161, 400, NULL, NULL, NULL, NULL, NULL)

Why is it doing an insert instead of just an update on the single  
attribute I want to change? Thanks!

---

<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:** [July 11, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/update-attribute-does-an-insert/109152/2 "2007-07-11T23:09:40Z")

</div>

> I am trying to update an integer value on a table using Rails, but it  
> complains that I am violating a not-null constraint because I didn’t  
> specify other attributes. My code is as follows:
> 
> sup = Supplier.new  
> sup.update\_attribute(‘num\_products’, total\_products)

You are creating a _new_ Supplier in ‘sup’. So there’s no associated  
record in the database for you to update the ‘num\_products’ column.

Try something like:

sup = Supplier.find(123)  
sup.update\_attribute(‘num\_products’, total\_products)

-philip

---

<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:** [July 11, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/update-attribute-does-an-insert/109152/3 "2007-07-11T23:43:46Z")

</div>

On Jul 11, 2007, at 5:23 PM, Philip H. wrote:

> Try something like:
> 
> > INSERT INTO suppliers(“name”, “num\_products”, “drop\_ship\_fee”,  
> > “real\_name”, “return\_policy”, shipping\_terms", “url”, “description”)  
> > VALUES(NULL, 161, 400, NULL, NULL, NULL, NULL, NULL)
> > 
> > Why is it doing an insert instead of just an update on the single  
> > attribute I want to change? Thanks!

If that’s really what you want to do, there are a couple other ways  
to go about it:

sup = Supplier.new(:num\_products =\> total\_products)

sup = Supplier.new do |s|  
s.num\_products = total\_products  
end

or if you want it to be on the disk NOW! you might want something like:

sup = Supplier.create(:name =\> “Supplier One”, :num\_products =\>  
total\_products)

or check out the find\_or\_create\_by\_\* methods (or even  
find\_or\_initialize\_by\_\*) if you might have a record, but are sure you  
will have one by the time the call is finished.

sup = Supplier.find\_or\_initialize\_by\_name(“Supplier One”) do |s|  
s.num\_products = total\_products  
# … other changes  
end

sup.save # writes the database once

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
