Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-11T14:22:37.650Z
Answers.Text: Yeah that's probably the best way for now 
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-11T20:25:11.097Z
Answers.Text: Thanks Mats, that sounds like a solid workaround! (And thanks Brandon for confirming.)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-11T12:03:32.413Z
Answers.LastActivityDate: 2016-08-11T13:37:50.437Z
Answers.LastEditDate: 2016-08-11T13:37:50.437Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <plugin-development><relations><records>
Title: Foreign Key column pointing to `craft_relations` table
ThreadText: # 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](https://github.com/pixelandtonic/Craft-Release/blob/master/app/records/BaseRecord.php#L565).
  
  A workaround is to override the [`createTable()`](https://github.com/pixelandtonic/Craft-Release/blob/master/app/records/BaseRecord.php#L239) method in your record class and add the foreign key constraint manually using [DbCommand](https://craftcms.com/docs/plugins/database):
  
  ```
  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 username_1, that sounds like a solid workaround! (And thanks username_2 for confirming.)  
  
  ---
  
  