1

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 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.

3
  • 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.
    – Brad Bell
    Commented Oct 26, 2015 at 17:22
  • Is your JWT token plugin for craft cms still alive? Been looking for an option like this, for a mobile apps to login.
    – bomanden
    Commented Aug 8, 2016 at 16:16
  • @bomanden I stuck a rough early version on GitHub 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!
    – Tam
    Commented Aug 9, 2016 at 9:42

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.