231
Views
14
Comments
Solved
How to solve the Insert Statement conflicted with the FOREIGN KEY constraint issue?

Hello everyone, 
I have an issue with uploading a file into a database.

The select file settings are shown below


Below is the upload button logic.

Here is the logic of the server action shown above



And this is the error


Just to inform, I have gone through 3 same questions and tried the solution but apparently I am still stuck. Also, I am trying to implement the best practice which is to show the server action inside the client action. I am quite confuse with how actually does the server action inside the server action works because I haven't found any videos on explaining that. I also read the documentation which I don't understand.

May somebody explain to me what actually is the problem?

2020-11-13 07-48-15
Khuong Truong
Solution

Hi @Hazim Jamal ,

As I see your action, is that you do assign after CreateOrUpdateFile_DSP1?

You need to do assignment before saving File_DSP1. 

Anw, can you show what do you pass as input of CreateOrUpdateFile_DSP1 in the action?

Regards,

Khuong

2024-12-04 07-24-25
Amol

ProjectID is primary here, So you might have already a record there.
So the solution is, Add a new column (Id) and make it as Primary key( Set as Identifier) and confirm once that ProjectID is not selected as unique key under entity property. 

UserImage.jpg
Hazim Jamal

I don't understand, the PROJECTID, in FILEDSP is an identifier of another entity.
And 1 Project would have 1 File called DSP.
Hence the one to one relationship between Project Entity and FileDSP entity.
Why would I want to make a new column to make a new Primary key?

2024-12-04 07-24-25
Amol

Ok. In this case, Before inserting a record you need to check if ProjectID is already exist, If yes update or else create it. Add this check 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Hazim,

Unfortunately, you do not show the full error message, so it's difficult to pinpoint exactly what the problem is. However, judging the part that's visible, the UploadFile entity has a mandatory ProjectId attribute that's a foreign key into the Project entity. And when you create or updaet the UploadFile entity, you do not set the value of ProjectId, so it's NullIdentifier(), which is not a valid value if an Id is mandatory.

UserImage.jpg
Hazim Jamal

Hye Killian, thank you for your response. Below is the full error message.

And on top of that, I would like to ask if my database is correct or even practicing the best way because I tried practicing the best way but it seems to make it harder when connecting the database to the front end side which I suppose leads to this kind of error.
To explain this database and why I did it. 

Talking about the static entity to entity relationship. I did the one to many relationship because 

For example, 
One Project Status (Static Entity) can connect to many Projects & 1 Project (Normal Entity) can connect to one project status (Static Entity). Hence the 1-to-many relationship. I suppose it is the right way to do so.

Talking about the Uploadfile Entity

For Example,

1 Project can have 1 FileSP (A type of file). Hence why the Identifier of each of those 4 entities on the right side of the picture, is an identifier of a Project.

Talking about the ProjectImportantPeople,
 
It's a many to many since 1 Project can have many project managers and 1 project manager can have many Projects. Hence the Many to many relationship and the extra table. Which I've learnt is right. 

The problem that I find is the uploadfile, since in my database later on when wanting to create aggregates, makes it a little difficult to understand. Would you mind explaining about this part. 

2020-11-13 07-48-15
Khuong Truong

Hi @Hazim Jamal ,

As my understanding, the error occurs because there is no project id value as you are assign to your FileDSP1.ProjectId in the Project table.

For example: you want to assign FileDSP1.ProjectID = 2 but in Project table the Project.ProjectID = 2 does not existed.

To solve your upload, I think you first save your Project table and get the newly created Project.ProjectID value and assign to your FileDSP1.ProjectID then save your FileDSP1.

Regards,

Khuong

UserImage.jpg
Hazim Jamal

Hye, how do I assign the newly created Project.ProjectID value to the FileDSP1.ProjectID?
Because I have already save the project in a previous page. 


2020-11-13 07-48-15
Khuong Truong

Hi @Hazim Jamal ,

As you have already save the project mean that you have ProjectID value right? 

You can get the ProjectID from CreateProject or CreateOrUpdateProject actions under Project Entity. Then you can play with this value such as pass as Input parameter to your Upload page...

Regards,

Khuong

UserImage.jpg
Hazim Jamal

Hye @Khuong,


In this here, how would I connect the ProjectID to this input parameter?

I'm still getting the same error, what part am I missing at?

UserImage.jpg
Hazim Jamal

Hye I'm sorry, I am very clueless. I don't see to understand what's happening

2024-12-04 07-24-25
Amol

Can U share your oml, If possible remove sensitive things and share a separate one?

2020-11-13 07-48-15
Khuong Truong
Solution

Hi @Hazim Jamal ,

As I see your action, is that you do assign after CreateOrUpdateFile_DSP1?

You need to do assignment before saving File_DSP1. 

Anw, can you show what do you pass as input of CreateOrUpdateFile_DSP1 in the action?

Regards,

Khuong

UserImage.jpg
Hazim Jamal

Hello @Khuong Truong,

Terribly sorry for the late reply, I have now found the solution thank you.

Regards,
Hazim

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.