Note that there are some explanatory texts on larger screens.

plurals
  1. POrails add_column with default value but the default value don't take effect
    text
    copied!<p>My rails version is 3.2.8 and use the default database. This is my migration code:</p> <pre><code>class AddQuantityToLineItem &lt; ActiveRecord::Migration def change add_column :line_items, :quantity, :integer,:default=&gt;1 end end </code></pre> <p><a href="https://stackoverflow.com/questions/4012871/how-do-default-0-and-null-false-differ-for-integer-fields-in-migrations">I find a explaination about :default option here </a>and as it said,when i create a new LineItem ,it should have a default quantity=1,but here is what i get from rails console:</p> <pre><code>lineb=LineItem.new #&lt;LineItem id: nil, product_id: nil, cart_id: nil, created_at: nil, updated_at: nil, quantity: nil&gt; </code></pre> <blockquote> <p>And when i get LineItem from the database,the quantity field is nil too.</p> </blockquote> <p>And here is the db/schema.rb :</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20121008065102) do create_table "carts", :force =&gt; true do |t| t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end create_table "line_items", :force =&gt; true do |t| t.integer "product_id" t.integer "cart_id" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false t.integer "quantity" end create_table "products", :force =&gt; true do |t| t.string "title" t.text "description" t.string "image_url" t.decimal "price", :precision =&gt; 8, :scale =&gt; 2 t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload