<p>I might be wrong, but I don't believe you'll be able to do that within the <code>defineRelations()</code> method – <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/records/BaseRecord.php#L565" rel="nofollow">Craft needs a record class for the table you want to create a relation with</a>.</p>

<p>A workaround is to override the <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/records/BaseRecord.php#L239" rel="nofollow"><code>createTable()</code></a> method in your record class and add the foreign key constraint manually using <a href="https://craftcms.com/docs/plugins/database" rel="nofollow">DbCommand</a>:</p>

<pre><code>public function createTable()
{
    parent::createTable();
    craft()-&gt;db-&gt;createCommand()-&gt;addForeignKey($this-&gt;getTableName(), 'id', 'relations', 'id', 'CASCADE', 'CASCADE');
}
</code></pre>

<p>To be perfectly honest the above feels quite hacky and there might be a better way – but it seems to work :)</p>

CC BY-SA 3.0
2016-08-11T14:22:37.650Z
Yeah that's probably the best way for now 
Brandon Kelly
CC BY-SA 3.0
2016-08-11T20:25:11.097Z
Thanks Mats, that sounds like a solid workaround! (And thanks Brandon for confirming.)
Lindsey D
CC BY-SA 3.0
2016-08-11T12:03:32.413Z
2016-08-11T13:37:50.437Z
2016-08-11T13:37:50.437Z
Mats Mikkel Rummelhoff
<p>I am writing a plugin with a Record. I want the primary key of the table to also be a <em>foreign key</em> relating to the <code>craft_relations</code> table. Ordinarily, I would add something like this...</p>

<pre><code>public function defineRelations()
{
    return array(
        'element' =&gt; array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' =&gt; true, 'onDelete' =&gt; static::CASCADE),
    );
}
</code></pre>

<p>However, it seems like the <code>craft_relations</code> table <strong>does not have a matching Record</strong>.</p>

<p>How can I add a primary key column which is also a foreign key?</p>

<plugin-development><relations><records>
Foreign Key column pointing to `craft_relations` table
# Foreign Key column pointing to `craft_relations` table

(Asked by: username_0 on 2016-08-11)

I am writing a plugin with a Record. I want the primary key of the table to also be a foreign key relating to the craft_relations table. Ordinarily, I would add something like this...
public function defineRelations()
{
    return array(
        'element' => array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE),
    );
}

However, it seems like the craft_relations table does not have a matching Record.
How can I add a primary key column which is also a foreign key?

**Tags:** plugin-development, relations, records


---

## 1 answers

### Answer by username_1 on 2016-08-11. Score: 5 | Accepted answer

I might be wrong, but I don't believe you'll be able to do that within the defineRelations() method – Craft needs a record class for the table you want to create a relation with.
A workaround is to override the createTable() method in your record class and add the foreign key constraint manually using DbCommand:
public function createTable()
{
    parent::createTable();
    craft()->db->createCommand()->addForeignKey($this->getTableName(), 'id', 'relations', 'id', 'CASCADE', 'CASCADE');
}

To be perfectly honest the above feels quite hacky and there might be a better way – but it seems to work :)

> **Comments:**
> **username_2**: Yeah that's probably the best way for now   
> **username_0**: Thanks Mats, that sounds like a solid workaround! (And thanks Brandon for confirming.)  

---

