Foreign Key constraint error on insert
Foreign Key constraint error on insert

I have a foreign key in a table that I am trying to insert into that registers a error.
Source of information
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblWinLoss_tblCompany". The conflict occurred in database "MyDatabase", table "dbo.tblCompany", column 'ID'. The statement has been terminated.
I am not updating more than one table. I want to update just tblWinLoss.BuilderID, and not touch tblCompany. tblWinLoss.BuilderID is nullable. I have, in my join table, set it as a int? data type.
I have also specified in my controller
.Field(new Field("Builders.ID").Set(false))
Where Builders is the alias for tblCompany, since I use multiple companies.
This insert doesn't seem to work unless I specify a value for tblWinLoss.BuilderID.
This question has an accepted answers - jump to answer
Answers
Hi,
Can you show me your full controller code please, and also the models if you are using any?
Finally, I'd also need to see the Javascript initialisation for Editor (sorry - lots of info I know, but it will be configuration specific).
Allan
Controller
View:
Model
Thank you. For
tblWinLoss.ID
in the Javascript try:If that doesn't fix it (I think it should) could you show me the full JSON response from the server when you do an edit or insert please?
Thanks,
Allan
That didn't work. I don't really have a problem with the Primary key, its with the BuilderID. I think I know what the problem is, and here is my debug query.
So, if you look at the debug value of tblWinLoss.BuilderID, its "", and when you run the insert
You get the error
But, if you actually had a NULL value, the insert works like it should.
So, how do we get it to put a null value instead of a empty string if no value is selected? It is a nullable integer, as specified in the model.
That looks similar, but not quite, to the SQL that the Editor .NET libraries create. Where are you getting those statements from - is it the SQL Server query log?
Can you show me the JSON returned from the server after a create submit as that should include the SQL Editor generates (since you have
.Debug(true)
)?Thanks,
Allan
That is the SQL from debug true. Also the error returned is above.
Just to clarify, the first one is the SQL from the debug(true) statement, which I got from the watch inside VS. The others are me showing what the value is from the binding @BuilderID = '' and what actually works, which is a NULL value.
I am not sure how to grab the JSON as its returned to the browser. I didn't see much in the network tab of the browser.
Is this what you are looking for?
I notes the type is set to NULL, not sure what that is, but the value not being NULL is the problem.
That was perfect - thank you. Change:
to be (i.e. add the setFormatter):
The reason that is required is that Editor submits values as plain http parameters, and there is no typing information. So there is no way to send
null
(we could send 'null', but that might be a string value!). We can use a set formatter on specific fields where we know an empty value is not going to be written into the db such as here.Documentation for that is available here.
Regards,
Allan
Perfect! So much to learn.