Answers.Body: <p>I've managed to fix the issue. Originally I was converting the Blob to a File, but this was causing the upload to silently fail. So, when Craft tried to create the relation between the asset field and the image, it threw the error above.</p>
  
  <p>Now, instead of converting to a file, I'm leaving the Blob as is but adding additional metadata (Last modified data &amp; name), and passing a file name to the <code>FormData.append()</code> when creating the FormData object.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-27T11:03:46.067Z
Answers.LastActivityDate: 2015-10-27T11:03:46.067Z
Answers.UserDisplayName: Tam
Body: <p>I'm getting the following error whenever I try to post a new entry to one of my channels in Craft:</p>
  
  <pre><code>2015/10/26 14:42:36 [error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'targetId' cannot be null. The SQL statement executed was: INSERT INTO `craft_relations` (`fieldId`, `sourceId`, `sourceLocale`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (:row0_col0, :row0_col1, NULL, NULL, :row0_col4, :row0_col5, :row0_col6, :row0_col7).
  </code></pre>
  
  <p>From what I can tell, the error is being triggered by the asset field in the section. I'm posting a JS created File to the asset field, not a file from a file input (which works fine during testing). The image file being posted is built from a Data URI which is converted to a Blob, and is then converted to a file. This is rather hacky*, but the best way I could find to successfully (as far as I can tell) post an image to Craft.</p>
  
  <p>Is it possible that some important metadata is being left out when the image file is created, that is causing this error? Or is it a case of the image not being saved before the entry in time for this relation to be created, or vice versa?</p>
  
  <p><em>*My reason for doing this is because the images are from a mobile phone camera, everything is being posted from a Cordova app.</em></p>
  
  <p><strong>Edit 1:</strong>
  From the app I'm posting directly to Craft with Ajax using the <code>entries/saveEntry</code> action. But this is being routed via <a href="https://github.com/ethercreative/TokenAuth" rel="nofollow">a plugin I wrote</a> to allow me to use JWT based authentication. I've had no issues with this during testing, only when I switched from using a field input to generating the files via JavaScript has this error started occurring.</p>
  
  <p><strong>Edit 1.5:</strong>
  I've managed to fix the issue. See below.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-26T17:22:09.390Z
Comments.Text: This is from the front-end and you're posting to a plugin controller? If so, it might help to share some of the plugin's logic as well as some of the relevant front-end code.
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-08T16:16:31.957Z
Comments.Text: Is your JWT token plugin for craft cms still alive? Been looking for an option like this, for a mobile apps to login.
Comments.UserDisplayName: bomanden
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-09T09:42:08.487Z
Comments.Text: @bomanden I stuck a [rough early version on GitHub](https://github.com/ethercreative/TokenAuth) ages ago. It's been a while since I last used it so it will probably need some tweaking to get working, but it's a good start. If I find some time I might continue developing it (we've got some apps coming down the line that might force me to), but I can't promise anything!
Comments.UserDisplayName: Tam
Tags: <assets><image><post><mysql>
Title: SQLSTATE[23000]: Column 'targetId' cannot be null
ThreadText: # SQLSTATE[23000]: Column 'targetId' cannot be null
  
  (Asked by: username_0 on 2015-10-26)
  
  I'm getting the following error whenever I try to post a new entry to one of my channels in Craft:
  
  ```
  2015/10/26 14:42:36 [error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'targetId' cannot be null. The SQL statement executed was: INSERT INTO `craft_relations` (`fieldId`, `sourceId`, `sourceLocale`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (:row0_col0, :row0_col1, NULL, NULL, :row0_col4, :row0_col5, :row0_col6, :row0_col7).
  
  ```
  
  From what I can tell, the error is being triggered by the asset field in the section. I'm posting a JS created File to the asset field, not a file from a file input (which works fine during testing). The image file being posted is built from a Data URI which is converted to a Blob, and is then converted to a file. This is rather hacky\*, but the best way I could find to successfully (as far as I can tell) post an image to Craft.
  
  Is it possible that some important metadata is being left out when the image file is created, that is causing this error? Or is it a case of the image not being saved before the entry in time for this relation to be created, or vice versa?
  
  *\*My reason for doing this is because the images are from a mobile phone camera, everything is being posted from a Cordova app.*
  
  **Edit 1:**
  From the app I'm posting directly to Craft with Ajax using the `entries/saveEntry` action. But this is being routed via [a plugin I wrote](https://github.com/ethercreative/TokenAuth) to allow me to use JWT based authentication. I've had no issues with this during testing, only when I switched from using a field input to generating the files via JavaScript has this error started occurring.
  
  **Edit 1.5:**
  I've managed to fix the issue. See below.
  
  **Tags:** assets, image, post, mysql
  
  > **Comments:**
  > **username_1**: This is from the front-end and you're posting to a plugin controller? If so, it might help to share some of the plugin's logic as well as some of the relevant front-end code.  
  > **username_2**: Is your JWT token plugin for craft cms still alive? Been looking for an option like this, for a mobile apps to login.  
  > **username_0**: @username_2 I stuck a [rough early version on GitHub](https://github.com/ethercreative/TokenAuth) ages ago. It's been a while since I last used it so it will probably need some tweaking to get working, but it's a good start. If I find some time I might continue developing it (we've got some apps coming down the line that might force me to), but I can't promise anything!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-10-27. Score: 2 | Accepted answer
  
  I've managed to fix the issue. Originally I was converting the Blob to a File, but this was causing the upload to silently fail. So, when Craft tried to create the relation between the asset field and the image, it threw the error above.
  
  Now, instead of converting to a file, I'm leaving the Blob as is but adding additional metadata (Last modified data & name), and passing a file name to the `FormData.append()` when creating the FormData object.
  
  
  ---
  
  