FOREIGN Key Constraint when uploading picture

FOREIGN Key Constraint when uploading picture

  

Hi there, 

I'm trying to do a To Do task with a picture upload. When I create a new To Do task and try to upload a new picture, it will show the following Foreign key error messages as shown in the screenshot. However there is no issue when I was editing an existing task and change a to new picture.
So in summary, I am able to change picture in existing task but not when creating a new task.

Attached is my oml for your kind review and help. Thanks a lot!

The ID field in the ToDoPicture entity is set to a data type of ToDo and is set to mandetory, this means that it is a link to the ToDo table rather than an auto increment ID of it's own so it must have a valid todo id before you can inset any records. 

So the problem is that when you are creating a NEW entry the record hasn't been inserted into the ToDo entity yet until you actually hit save however you have triggered the ChangePicture action which is trying to insert into the ToDoPicture entity but the ID is null.


John Williams wrote:

The ID field in the ToDoPicture entity is set to a data type of ToDo and is set to mandetory, this means that it is a link to the ToDo table rather than an auto increment ID of it's own so it must have a valid todo id before you can inset any records. 

So the problem is that when you are creating a NEW entry the record hasn't been inserted into the ToDo entity yet until you actually hit save however you have triggered the ChangePicture action which is trying to insert into the ToDoPicture entity but the ID is null.


Hi John,


Thanks for your reply. So do you mean that I should set my ID field of ToDoPicture entity to an auto increment ID to solve the issue? 


Solution

You have a couple of different options. 

1, Create the ToDo record if it doesn't exist before storing the picture. This would mean that you then have a valid ID to pass through. However it's not very "nice" programming as you then need do a cleanup and delete the record if the user hits cancel and you need to ensure you don't update the ToDo record if it is an edit, I don't generally like the idea of creating a record until it is actually saved.

2, Have an auto increment ID on ToDoPicture and a non mandatory link to ToDoID. Insert the record in ToDoPicture with the ID (it will be null on a new record) and then update the ID on ToDoPicture when you hit save. This will require your code to "remember" the ID from ToDoPicture for the update on the save

3, If you only have a single picture for any ToDo item then put the link to ToDoPicture in ToDo itself and have an Auto increment id in ToDoPicture. This is an easy way but is a one to one link so limits you to a single record.

On all 3 options you may need to look at doing a cleanup of the ToDoPicture record if the user hits cancel unless you don't mid a number of orphaned picture records.

As a side point, I generally recommend always having an auto increment ID on every table anyway even if you don't think you have a direct need for it, Outsystems will try to do this by default.

Solution

John Williams wrote:

You have a couple of different options. 

1, Create the ToDo record if it doesn't exist before storing the picture. This would mean that you then have a valid ID to pass through. However it's not very "nice" programming as you then need do a cleanup and delete the record if the user hits cancel and you need to ensure you don't update the ToDo record if it is an edit, I don't generally like the idea of creating a record until it is actually saved.

2, Have an auto increment ID on ToDoPicture and a non mandatory link to ToDoID. Insert the record in ToDoPicture with the ID (it will be null on a new record) and then update the ID on ToDoPicture when you hit save. This will require your code to "remember" the ID from ToDoPicture for the update on the save

3, If you only have a single picture for any ToDo item then put the link to ToDoPicture in ToDo itself and have an Auto increment id in ToDoPicture. This is an easy way but is a one to one link so limits you to a single record.

On all 3 options you may need to look at doing a cleanup of the ToDoPicture record if the user hits cancel unless you don't mid a number of orphaned picture records.

As a side point, I generally recommend always having an auto increment ID on every table anyway even if you don't think you have a direct need for it, Outsystems will try to do this by default.

Hi John,

I've tried to create a new oml to do a test on upload widget. And the problem I face now is, when I edit my to do or create a new to do in my Do To Details, after I save, the FileName and File Type doesn't seems to be updated in my To Do main page. I can't think of any part I overlook in my oml. Able to help? Thanks