Answers.Body: <p>I think the problem is this:</p>
  
  <pre><code>&lt;option value="v.id"&gt;{{v.title}}&lt;/option&gt;
  </code></pre>
  
  <p>Try changing it to:</p>
  
  <pre><code>&lt;option value="{{ v.id }}"&gt;{{v.title}}&lt;/option&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-16T03:22:57.800Z
Answers.Text: aarg! Hope this will help someone else with a 'foreign key constraint' sql error.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-16T03:06:51.610Z
Answers.LastActivityDate: 2016-02-16T03:06:51.610Z
Answers.UserDisplayName: Brad Bell
Body: <p>I am getting a foreign key constraint sql error when trying to add an entry through a front end form.</p>
  
  <pre><code>&lt;form method="post" accept-charset="UTF-8"&gt;
    {{ getCsrfInput() }}
    &lt;input type="hidden" name="action" value="entries/saveEntry"&gt;
    &lt;input type="hidden" name="entryId" value="{{ entry.id }}"&gt;
    &lt;select name="fields[vendor][]"&gt;
      {% for v in craft.entries.section('vendors') %}
        &lt;option value="v.id"&gt;{{v.title}}&lt;/option&gt;
      {% endfor %}
    &lt;/select&gt;
    &lt;input type="submit" value="Submit"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><code>vendor</code> is an Entries fieldtype, for unlimited entries from the <code>vendors</code> channel. Very similar code for adding a category works fine.</p>
  
  <p>The error (complete with back-ticks):</p>
  
  <blockquote>
    <p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>metro</code>.<code>craft_relations</code>, CONSTRAINT <code>craft_relations_targetId_fk</code> FOREIGN KEY (<code>targetId</code>) REFERENCES <code>craft_elements</code> (<code>id</code>) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO <code>craft_relations</code> (<code>fieldId</code>, <code>sourceId</code>, <code>sourceLocale</code>, <code>targetId</code>, <code>sortOrder</code>, <code>dateCreated</code>, <code>dateUpdated</code>, <code>uid</code>) VALUES (:row0_col0, :row0_col1, NULL, :row0_col3, :row0_col4, :row0_col5, :row0_col6, :row0_col7). Bound with :row0_col0='13', :row0_col1='39', :row0_col3='v.id', :row0_col4=1, :row0_col5='2016-02-13 08:28:03', :row0_col6='2016-02-13 08:28:03', :row0_col7='e455a06e-8f9d-4299-ad57-93577907f9a0' </p>
  </blockquote>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-02-13T03:33:21.527Z
Comments.Text: Can you share the actual FK constraint error?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-02-13T08:30:42.467Z
Comments.Text: @BradBell added
Comments.UserDisplayName: Marion Newlevant
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-02-15T00:08:49.967Z
Comments.Text: Are there any other fields in the `vendors` channel?  Also, could you enable devMode and share the stack trace leading up to the error?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-02-15T03:33:50.290Z
Comments.Text: @BradBell Filed a support ticket, since you are implying this is a bug.
Comments.UserDisplayName: Marion Newlevant
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-02-16T03:01:07.387Z
Comments.Text: `since you are implying this is a bug.` - well, just trying to figure out what's going on. :) I think I see though so adding an answer.
Comments.UserDisplayName: Brad Bell
Tags: <form><saveentry>
Title: Foreign key constraint sql error from entries/saveEntry when adding entry
ThreadText: # Foreign key constraint sql error from entries/saveEntry when adding entry
  
  (Asked by: username_0 on 2016-02-13)
  
  I am getting a foreign key constraint sql error when trying to add an entry through a front end form.
  
  ```
  <form method="post" accept-charset="UTF-8">
    {{ getCsrfInput() }}
    <input type="hidden" name="action" value="entries/saveEntry">
    <input type="hidden" name="entryId" value="{{ entry.id }}">
    <select name="fields[vendor][]">
      {% for v in craft.entries.section('vendors') %}
        <option value="v.id">{{v.title}}</option>
      {% endfor %}
    </select>
    <input type="submit" value="Submit"/>
  </form>
  
  ```
  
  `vendor` is an Entries fieldtype, for unlimited entries from the `vendors` channel. Very similar code for adding a category works fine.
  
  The error (complete with back-ticks):
  
  > CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`metro`.`craft_relations`, CONSTRAINT `craft_relations_targetId_fk` FOREIGN KEY (`targetId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_relations` (`fieldId`, `sourceId`, `sourceLocale`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (:row0\_col0, :row0\_col1, NULL, :row0\_col3, :row0\_col4, :row0\_col5, :row0\_col6, :row0\_col7). Bound with :row0\_col0='13', :row0\_col1='39', :row0\_col3='v.id', :row0\_col4=1, :row0\_col5='2016-02-13 08:28:03', :row0\_col6='2016-02-13 08:28:03', :row0\_col7='e455a06e-8f9d-4299-ad57-93577907f9a0'
  
  **Tags:** form, saveentry
  
  > **Comments:**
  > **username_1**: Can you share the actual FK constraint error?  
  > **username_0**: @username_1 added  
  > **username_1**: Are there any other fields in the `vendors` channel?  Also, could you enable devMode and share the stack trace leading up to the error?  
  > **username_0**: @username_1 Filed a support ticket, since you are implying this is a bug.  
  > **username_1**: `since you are implying this is a bug.` - well, just trying to figure out what's going on. :) I think I see though so adding an answer.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-02-16. Score: 1 | Accepted answer
  
  I think the problem is this:
  
  ```
  <option value="v.id">{{v.title}}</option>
  
  ```
  
  Try changing it to:
  
  ```
  <option value="{{ v.id }}">{{v.title}}</option>
  
  ```
  
  > **Comments:**
  > **username_0**: aarg! Hope this will help someone else with a 'foreign key constraint' sql error.  
  
  ---
  
  