<p>You best bet would be to start by knowing all the exisiting default attribute types in this file:</p>

<p><code>craft/app/enums/AttributeType.php</code></p>

<p>Then look in</p>

<p><code>craft/app/helpers/ModelHelper.php</code>
and
<code>craft/app/helpers/DbHelper.php</code></p>

<p>There you will find all sorts of properties for column types and their default configs options like maxLength. Here is all the ColumnTypes defaults:</p>

<pre><code>public static $attributeTypeDefaults = array(
    AttributeType::Mixed      =&gt; array('model' =&gt; null, 'column' =&gt; ColumnType::Text),
    AttributeType::Bool       =&gt; array('maxLength' =&gt; 1, 'default' =&gt; false, 'required' =&gt; true, 'column' =&gt; ColumnType::TinyInt, 'unsigned' =&gt; true),
    AttributeType::ClassName  =&gt; array('maxLength' =&gt; 150, 'column' =&gt; ColumnType::Varchar),
    AttributeType::DateTime   =&gt; array('column' =&gt; ColumnType::DateTime),
    AttributeType::Email      =&gt; array('minLength' =&gt; 5, 'column' =&gt; ColumnType::Varchar),
    AttributeType::Enum       =&gt; array('values' =&gt; array(), 'column' =&gt; ColumnType::Enum),
    AttributeType::Handle     =&gt; array('maxLength' =&gt; 255, 'reservedWords' =&gt; 'id,dateCreated,dateUpdated,uid,title', 'column' =&gt; ColumnType::Varchar),
    AttributeType::Locale     =&gt; array('column' =&gt; ColumnType::Locale),
    AttributeType::Name       =&gt; array('maxLength' =&gt; 255, 'column' =&gt; ColumnType::Varchar),
    AttributeType::Number     =&gt; array('min' =&gt; null, 'max' =&gt; null, 'decimals' =&gt; 0),
    AttributeType::SortOrder  =&gt; array('column' =&gt; ColumnType::TinyInt),
    AttributeType::Template   =&gt; array('maxLength' =&gt; 500, 'column' =&gt; ColumnType::Varchar),
    AttributeType::Url        =&gt; array('maxLength' =&gt; 255, 'column' =&gt; ColumnType::Varchar),
);
</code></pre>

<p>You will also see in the DbHelper.php how the column types map into the mysql definition of the table like so:</p>

<pre><code>case ColumnType::Decimal:
            {
                $def = 'DECIMAL('.$config['length'].','.$config['decimals'].')';
                break;
            }
</code></pre>

<p>Hope that helps. I would encourage you to look at all the <code>defineAttributes()</code> methods in the <code>craft/app/records</code> directory.</p>

CC BY-SA 3.0
2015-01-15T12:15:31.190Z
Thanks Luke thats a big help, how do I get my PK field to auto increament? I have noticed that a uid field gets added how is this used?
Gary Constable
CC BY-SA 3.0
2015-01-15T12:24:05.937Z
No worries. I suppose it should be documented better but most of the defaults are fine anyway so just using the default attribute types is fine.
Luke Holder
CC BY-SA 3.0
2015-01-15T14:48:14.410Z
PK id already auto increments by default you don't need to add id to defineAttributes it gets added by craft on install.
Luke Holder
CC BY-SA 3.0
2015-01-15T16:55:40.917Z
@GaryConstable Craft adds and uses the uid column internally. No need to worry about it.
Brad Bell
CC BY-SA 3.0
2015-03-27T16:20:39.013Z
I'm late to the party, but I'm also working through validation/records etc. I found [this article](https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-2-attributetypes) to be quite helpful as well.
Damon
CC BY-SA 3.0
2015-09-02T18:07:44.890Z
For further reference, here's a list of all the possible `AttributeType::*` constants (the above list from `ModelHelper.php` doesn't include all the possibilities): https://github.com/pixelandtonic/Craft-Release/blob/master/app/enums/AttributeType.php
Phil Gyford
CC BY-SA 3.0
2015-01-15T12:06:31.890Z
2015-01-15T12:06:31.890Z
Luke Holder
<p>I have just started to build my first plugin with Craft CMS.</p>

<p>Im at the part where I want to defne my database table.</p>

<p><a href="http://buildwithcraft.com/docs/plugins/database" rel="noreferrer">http://buildwithcraft.com/docs/plugins/database</a></p>

<p>For the method definedAttributes() what values will the array accept.</p>

<p>It looks like I can define values and types, but how do i add field lengths, primarykeys etc.. </p>

<p>Are there some other docs that I can look at?</p>

<p>Thanks for your help.</p>

<plugin-development>
ActiveRecord defineAttributes()
# ActiveRecord defineAttributes()

(Asked by: username_0 on 2015-01-15)

I have just started to build my first plugin with Craft CMS.

Im at the part where I want to defne my database table.

<http://buildwithcraft.com/docs/plugins/database>

For the method definedAttributes() what values will the array accept.

It looks like I can define values and types, but how do i add field lengths, primarykeys etc..

Are there some other docs that I can look at?

Thanks for your help.

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2015-01-15. Score: 9 | Accepted answer

You best bet would be to start by knowing all the exisiting default attribute types in this file:
craft/app/enums/AttributeType.php
Then look in
craft/app/helpers/ModelHelper.php
and
craft/app/helpers/DbHelper.php
There you will find all sorts of properties for column types and their default configs options like maxLength. Here is all the ColumnTypes defaults:
public static $attributeTypeDefaults = array(
    AttributeType::Mixed      => array('model' => null, 'column' => ColumnType::Text),
    AttributeType::Bool       => array('maxLength' => 1, 'default' => false, 'required' => true, 'column' => ColumnType::TinyInt, 'unsigned' => true),
    AttributeType::ClassName  => array('maxLength' => 150, 'column' => ColumnType::Varchar),
    AttributeType::DateTime   => array('column' => ColumnType::DateTime),
    AttributeType::Email      => array('minLength' => 5, 'column' => ColumnType::Varchar),
    AttributeType::Enum       => array('values' => array(), 'column' => ColumnType::Enum),
    AttributeType::Handle     => array('maxLength' => 255, 'reservedWords' => 'id,dateCreated,dateUpdated,uid,title', 'column' => ColumnType::Varchar),
    AttributeType::Locale     => array('column' => ColumnType::Locale),
    AttributeType::Name       => array('maxLength' => 255, 'column' => ColumnType::Varchar),
    AttributeType::Number     => array('min' => null, 'max' => null, 'decimals' => 0),
    AttributeType::SortOrder  => array('column' => ColumnType::TinyInt),
    AttributeType::Template   => array('maxLength' => 500, 'column' => ColumnType::Varchar),
    AttributeType::Url        => array('maxLength' => 255, 'column' => ColumnType::Varchar),
);

You will also see in the DbHelper.php how the column types map into the mysql definition of the table like so:
case ColumnType::Decimal:
            {
                $def = 'DECIMAL('.$config['length'].','.$config['decimals'].')';
                break;
            }

Hope that helps. I would encourage you to look at all the defineAttributes() methods in the craft/app/records directory.

> **Comments:**
> **username_0**: Thanks Luke thats a big help, how do I get my PK field to auto increament? I have noticed that a uid field gets added how is this used?  
> **username_1**: No worries. I suppose it should be documented better but most of the defaults are fine anyway so just using the default attribute types is fine.  
> **username_1**: PK id already auto increments by default you don't need to add id to defineAttributes it gets added by craft on install.  
> **username_2**: @GaryConstable Craft adds and uses the uid column internally. No need to worry about it.  
> **username_3**: I'm late to the party, but I'm also working through validation/records etc. I found [this article](https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-2-attributetypes) to be quite helpful as well.  
> **username_4**: For further reference, here's a list of all the possible `AttributeType::*` constants (the above list from `ModelHelper.php` doesn't include all the possibilities): https://github.com/pixelandtonic/Craft-Release/blob/master/app/enums/AttributeType.php  

---

