<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>

CC BY-SA 3.0
2015-01-15T14:13:34.233Z
2015-01-15T14:13:34.233Z
Jay
<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>

CC BY-SA 3.0
2015-01-15T13:37:28.593Z
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?
Gary Constable
CC BY-SA 3.0
2015-01-15T14:49:02.420Z
why do you need to increment a non PK?
Luke Holder
CC BY-SA 3.0
2015-01-15T14:51:31.830Z
No I dont, I just dont want to be forced to call every PK field id.
Gary Constable
CC BY-SA 3.0
2015-01-15T14:56:23.057Z
no worries, its just a convention that works for most people. Out of interest, why do you prefer something other than id?
Luke Holder
CC BY-SA 3.0
2015-01-15T15:16:32.400Z
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 :)
Gary Constable
CC BY-SA 3.0
2015-01-15T16:57:54.963Z
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?
Luke Holder
CC BY-SA 3.0
2015-01-16T17:21:37.543Z
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. :)
Gary Constable
CC BY-SA 3.0
2015-11-01T18:22:17.747Z
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.
H2ONOCK
CC BY-SA 4.0
2019-02-20T23:16:39.633Z
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?
efatsi
CC BY-SA 3.0
2015-01-15T13:32:59.460Z
2015-01-15T13:32:59.460Z
Luke Holder
<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>

<plugin-development>
ActiveRecord Primary Key - Auto increment
# 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 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?  
> **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 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?  
> **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 @LukeHolder?  

---

