Can't set NULL default value
Can't set NULL default value
chris.cavage
Posts: 46Questions: 12Answers: 0
in Editor
My DB is set to accept null values for this field in particular, but as I coded the below script, the default value entered is '0' and not null.
var editor_todos = new $.fn.dataTable.Editor({
ajax: 'php/table.user_todos.php?user_id=' + user_id,
table: '#user_todos',
fields: [
{
"label": "Assign To Staff:",
"name": "assigned_to_user_id",
"type": "select2",
"opts": {
"ajax" { my ajax url code and script },
"allowClear": true,
"placeholder": {
"id": "",
"text": "(you)"
}
}
Any idea why the default is not being saved as NULL instead of 0, as it is now? I'm wondering if it has something to do with select2.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
If you have a look at the "Headers" section of the Ajax request that is being sent to the server in your browser's Network inspector - could you show me the parameters that are being sent please?
Thanks,
Allan
I suspect that you may be running into a MySQL scenario. Sending '0' or an empty value will not result in a default of null.
Use "ifEmpty(null)" as a formatter for the field(s) in question.
Hey Allan: here's the data from the form that's being sent:
In the past, if I've submitted something like this to my DB, it does
insert assigned_to_contact_id as NULL. In this insert, location_id does enter as NULL. assigned_to_user_id and assigned_to_contact_id enter as 0. Both of these are using the select2.
location_id for right now is just a text input.
Tangerine, this works per your suggestion:
I have to use the formatter in this way for any field that is using the select2 plugin. The NULL values are entered in my DB instead of 0 values.
If you understand why this is necessary, I'd be curious to know! Thank you.
I don't use select2, so I can't say if it makes a significant effect in this context.
I used to get problems with null defaults in MySQL, and from what I remember any value supplied (other than null) will not default. "0" is regarded as a legitimate value, and less obviously "" (empty) is also regarded as legitimate. You have to explicitly submit null, or not submit the field at all.
My memory might not be perfect - I recommend you check it out for yourself!
EDIT: There's also the PHP factor - "0" is considered empty.
Yup - Editor submits the data from the form as HTTP parameters. They are basically just strings with zero typing information, so if you want an empty string to appear as null, then you need to use a formatter for it.
Allan
Oh, I see! Thank you! That all makes sense. I understand. Great support!