<p>For <code>NOT NULL</code> use <code>'required' =&gt; true</code> and to default to 1 use <code>'default' =&gt; 1</code>.</p>

CC BY-SA 3.0
2015-06-22T05:00:24.583Z
2015-06-22T05:30:23.150Z
2015-06-22T05:30:23.150Z
Brad Bell
<p>How to Use defineAttributes  To Specify NOT NULL and DEFAULT '1' in MySQL Record</p>

<p>I am porting a plugin from another framework and it has the following
MySql tables structure. Is there an easy way to create the same
table structure within craft plugin?</p>

<pre>
CREATE TABLE IF NOT EXISTS `exp_shortcut_shortcuts` (
`shortcut_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`site_id` smallint(3) unsigned NOT NULL default '1',
`entry_id` smallint(3) unsigned NOT NULL default '0',
`field_id` smallint(3) unsigned NOT NULL default '0',
`autogenerated` char(1) NOT NULL default 'y',
`shortcut` varchar(100) NOT NULL default '',
`full_url_model` varchar(255) NOT NULL default '',
`full_url` varchar(255) NOT NULL default '',
`entry_date` int(11) unsigned NOT NULL default '0',
`edit_date` int(11) unsigned NOT NULL default '0',
PRIMARY KEY (`shortcut_id`),
INDEX (`shortcut`)
) ;;

namespace Craft;

class ShortcutRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'shortcut_shortcuts';
    }

    public function primaryKey()
    {
       return 'shortcut_id';
    }

    protected function defineAttributes()
    {
        return array(
            'shortcut_id' => array(AttributeType::Number, 'column' => ColumnType::PK),
            'site_id' => array(AttributeType::Number, 'column' => ColumnType::TinyInt),
</pre>                      

<p>I see how to add TinyInt. How do I specify NOT NULL and default '1'? </p>

<plugin-development><database>
How to Use defineAttributes To Specify NOT NULL and DEFAULT '1' in MySQL Record
# How to Use defineAttributes To Specify NOT NULL and DEFAULT '1' in MySQL Record

(Asked by: username_0 on 2015-06-22)

How to Use defineAttributes  To Specify NOT NULL and DEFAULT '1' in MySQL Record
I am porting a plugin from another framework and it has the following
MySql tables structure. Is there an easy way to create the same
table structure within craft plugin?

CREATE TABLE IF NOT EXISTS `exp_shortcut_shortcuts` (
`shortcut_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`site_id` smallint(3) unsigned NOT NULL default '1',
`entry_id` smallint(3) unsigned NOT NULL default '0',
`field_id` smallint(3) unsigned NOT NULL default '0',
`autogenerated` char(1) NOT NULL default 'y',
`shortcut` varchar(100) NOT NULL default '',
`full_url_model` varchar(255) NOT NULL default '',
`full_url` varchar(255) NOT NULL default '',
`entry_date` int(11) unsigned NOT NULL default '0',
`edit_date` int(11) unsigned NOT NULL default '0',
PRIMARY KEY (`shortcut_id`),
INDEX (`shortcut`)
) ;;

namespace Craft;

class ShortcutRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'shortcut_shortcuts';
    }

    public function primaryKey()
    {
       return 'shortcut_id';
    }

    protected function defineAttributes()
    {
        return array(
            'shortcut_id' => array(AttributeType::Number, 'column' => ColumnType::PK),
            'site_id' => array(AttributeType::Number, 'column' => ColumnType::TinyInt),

I see how to add TinyInt. How do I specify NOT NULL and default '1'?

**Tags:** plugin-development, database

---

## 1 answers

### Answer by username_1 on 2015-06-22. Score: 3

For `NOT NULL` use `'required' => true` and to default to 1 use `'default' => 1`.


---

