Answers.Body: <p>If you want to add a default to an existing column in a table in a migration, you'd do something like this:</p>
  
  <pre><code>$this-&gt;alterColumn('tableName', 'columnName', array('column' =&gt; ColumnType::Number, 'default' =&gt; 0));
  </code></pre>
  
  <p>If you want to add a default to a new column in a table in a migration, you'd do something like this:</p>
  
  <pre><code>$this-&gt;addColumn('tableName', 'columnName', array('column' =&gt; ColumnType::Number, 'default' =&gt; 0));
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-08T03:12:55.927Z
Answers.LastActivityDate: 2016-04-08T03:12:55.927Z
Answers.UserDisplayName: Brad Bell
Body: <p>I've got a default value defined in my model for a field</p>
  
  <pre><code>'limit' =&gt; array(AttributeType::Number, 'default' =&gt; 0)
  </code></pre>
  
  <p>When I create a new record, if the limit field is empty, <code>NULL</code> gets stored in the DB instead of 0. I thought <code>$model->setAttributes()</code> would set the default values if the values coming in were <code>NULL</code>. What am I missing?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-03T12:30:00.537Z
Comments.Text: Did you define the default value for the attribute in your record as well?
Comments.UserDisplayName: Steve Rowling
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-03T14:10:26.543Z
Comments.Text: Yes, it's on the record as well.
Comments.UserDisplayName: Trevor Davis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-03T22:08:19.490Z
Comments.Text: Does the `limit` column in the database have a default set?  Did the record create the table?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-04T13:36:51.693Z
Comments.Text: Ahhh nope because this column was added in a DB migration. How can I set the default value in a migration?
Comments.UserDisplayName: Trevor Davis
Tags: <plugin-development><models><records>
Title: Plugin Use Default Value When Empty
ThreadText: # Plugin Use Default Value When Empty
  
  (Asked by: username_0 on 2016-04-02)
  
  I've got a default value defined in my model for a field
  
  ```
  'limit' => array(AttributeType::Number, 'default' => 0)
  
  ```
  
  When I create a new record, if the limit field is empty, `NULL` gets stored in the DB instead of 0. I thought `$model->setAttributes()` would set the default values if the values coming in were `NULL`. What am I missing?
  
  **Tags:** plugin-development, models, records
  
  > **Comments:**
  > **username_1**: Did you define the default value for the attribute in your record as well?  
  > **username_0**: Yes, it's on the record as well.  
  > **username_2**: Does the `limit` column in the database have a default set?  Did the record create the table?  
  > **username_0**: Ahhh nope because this column was added in a DB migration. How can I set the default value in a migration?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-04-08. Score: 0 | Accepted answer
  
  If you want to add a default to an existing column in a table in a migration, you'd do something like this:
  
  ```
  $this->alterColumn('tableName', 'columnName', array('column' => ColumnType::Number, 'default' => 0));
  
  ```
  
  If you want to add a default to a new column in a table in a migration, you'd do something like this:
  
  ```
  $this->addColumn('tableName', 'columnName', array('column' => ColumnType::Number, 'default' => 0));
  
  ```
  
  
  ---
  
  