Answers.Body: <p>Use a 'column' in your defineAttributes:</p>
  
  <p>e.g</p>
  
  <pre><code>'id_customer'       =&gt; array(AttributeType::Number, 'column' =&gt; ColumnType::PK)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T14:13:34.233Z
Answers.LastActivityDate: 2015-01-15T14:13:34.233Z
Answers.UserDisplayName: Jay
Answers.Body: <p>You don't need to define an <code>id</code> attribute in your Craft Records Class - it is created automatically for you on plugin install as an auto-incremented int. You do need to define the id attribute on the equivalent Model though, and <code>AttributeType::Number</code> is fine for that.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T13:37:28.593Z
Answers.Text: thanks luke - my next question is, whats public function primaryKey() for, what if i needed to call my PK field id_customer and auto increment that?
Answers.UserDisplayName: Gary Constable
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T14:49:02.420Z
Answers.Text: why do you need to increment a non PK?
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T14:51:31.830Z
Answers.Text: No I dont, I just dont want to be forced to call every PK field id.
Answers.UserDisplayName: Gary Constable
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T14:56:23.057Z
Answers.Text: no worries, its just a convention that works for most people. Out of interest, why do you prefer something other than id?
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T15:16:32.400Z
Answers.Text: when I look at a table if I see something like customer_id in the customer_address table, I know its a join, it just makes sence to me :)
Answers.UserDisplayName: Gary Constable
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T16:57:54.963Z
Answers.Text: Hey Gary yes a foreign key to the customer table from the customer_address table makes sense as customer_id but why would you auto increment the foreign key?
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-16T17:21:37.543Z
Answers.Text: say what? Im incrementing the primary key, using he reply from @Jay we have managed to change the primary key name from id to a name of our choice. :)
Answers.UserDisplayName: Gary Constable
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-01T18:22:17.747Z
Answers.Text: Quick note on this... if you've used defineRelations or defineIndexes then the automatic creation of an id Primary Key that auto_increments can be compromised. Always worth checking to see if this is the case.
Answers.UserDisplayName: H2ONOCK
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-20T23:16:39.633Z
Answers.Text: I know this is an old issue, but I'm running into an error: `Field 'id' doesn't have a default value` when trying to save a custom record to the database. It seems the `id` field is not defined to `auto_increment`. I'm building a pair of models/records/migrations similar to the Commerce Cart + LineItem combo. Any thoughts @LukeHolder?
Answers.UserDisplayName: efatsi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-15T13:32:59.460Z
Answers.LastActivityDate: 2015-01-15T13:32:59.460Z
Answers.UserDisplayName: Luke Holder
Body: <p>With Craft Active Record, using the definedAttributes Method how do I set my Primary Key Field to auto increment?</p>
  
  <p>Or does it just calculate another number for the field that has not been used when the record gets saved?</p>
  
  <p>Thanks for your help.</p>
  
Tags: <plugin-development>
Title: ActiveRecord Primary Key - Auto increment
ThreadText: # ActiveRecord Primary Key - Auto increment
  
  (Asked by: username_0 on 2015-01-15)
  
  With Craft Active Record, using the definedAttributes Method how do I set my Primary Key Field to auto increment?
  
  Or does it just calculate another number for the field that has not been used when the record gets saved?
  
  Thanks for your help.
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-01-15. Score: 1 | Accepted answer
  
  Use a 'column' in your defineAttributes:
  
  e.g
  
  ```
  'id_customer'       => array(AttributeType::Number, 'column' => ColumnType::PK)
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2015-01-15. Score: 2
  
  You don't need to define an `id` attribute in your Craft Records Class - it is created automatically for you on plugin install as an auto-incremented int. You do need to define the id attribute on the equivalent Model though, and `AttributeType::Number` is fine for that.
  
  > **Comments:**
  > **username_0**: thanks username_2 - my next question is, whats public function primaryKey() for, what if i needed to call my PK field id_customer and auto increment that?  
  > **username_2**: why do you need to increment a non PK?  
  > **username_0**: No I dont, I just dont want to be forced to call every PK field id.  
  > **username_2**: no worries, its just a convention that works for most people. Out of interest, why do you prefer something other than id?  
  > **username_0**: when I look at a table if I see something like customer_id in the customer_address table, I know its a join, it just makes sence to me :)  
  > **username_2**: Hey username_0 yes a foreign key to the customer table from the customer_address table makes sense as customer_id but why would you auto increment the foreign key?  
  > **username_0**: say what? Im incrementing the primary key, using he reply from @username_1 we have managed to change the primary key name from id to a name of our choice. :)  
  > **username_3**: Quick note on this... if you've used defineRelations or defineIndexes then the automatic creation of an id Primary Key that auto_increments can be compromised. Always worth checking to see if this is the case.  
  > **username_4**: I know this is an old issue, but I'm running into an error: `Field 'id' doesn't have a default value` when trying to save a custom record to the database. It seems the `id` field is not defined to `auto_increment`. I'm building a pair of models/records/migrations similar to the Commerce Cart + LineItem combo. Any thoughts @username_2?  
  
  ---
  
  