ISJSON constraints should not be escaped.
wpostma
Posts: 22
Your BRACKET_ALL_THE_THINGS() function needs an EXCEPTION encoded so that JSON validation Constraints like this do not get rewritten:
ALTER TABLE [PHI].[Study] ADD CONSTRAINT [PHI_STUDY_ExtJSON_VALID] CHECK (isjson([ExtJSON])>0)
It appears that something inside SQL Source control is rewriting the above so that it breaks when executing, it writes the following BAD T-SQL instead:
ALTER TABLE [PHI].[Study] ADD CONSTRAINT [STUDY_ExtJSON_VALID] CHECK (([isjson]([ExtJSON])>(0)))
Look at that. the function isjson should NOT have [] around it, and the several extra pairs of parenthesis are pathological too.
Not EVERYTHING matching an [a-zA-Z]* regex inside a CHECK() expression can be assumed to be a literal field reference!
Probably you have some logic that tries to be smart about this, but the new JSON constraint functions in SQL 2016 are not one of the things you handle correctly.
Warren
ALTER TABLE [PHI].[Study] ADD CONSTRAINT [PHI_STUDY_ExtJSON_VALID] CHECK (isjson([ExtJSON])>0)
It appears that something inside SQL Source control is rewriting the above so that it breaks when executing, it writes the following BAD T-SQL instead:
ALTER TABLE [PHI].[Study] ADD CONSTRAINT [STUDY_ExtJSON_VALID] CHECK (([isjson]([ExtJSON])>(0)))
Look at that. the function isjson should NOT have [] around it, and the several extra pairs of parenthesis are pathological too.
Not EVERYTHING matching an [a-zA-Z]* regex inside a CHECK() expression can be assumed to be a literal field reference!
Probably you have some logic that tries to be smart about this, but the new JSON constraint functions in SQL 2016 are not one of the things you handle correctly.
Warren
Comments
We've opened a ticket for this issue so we can troubleshoot further. I'll be responding to you through that ticket immediatly after this forum response, thanks.
Red Gate Software
US Product Support
After that commit, go to another working copy (another user's machine with SQL Source control) and try to fetch the changes and apply them.
After that, run this tsql to add the constraint which contains an expression in the form ( isjson([ExtJSON])> 0):
Now commit that change on working copy on workstation 1. This commit should only contain the above extjson validation constraint and its check enable.
Now go back to workstation 2 and try to update.
Expectation:
ALTER TABLE [dbo].[Group] WITH CHECK ADD CONSTRAINT [GROUP_ExtJSON_VALID] CHECK ( isjson([ExtJSON])> 0)
GO
Actual:
ALTER TABLE [dbo].[Group] WITH CHECK ADD CONSTRAINT [GROUP_ExtJSON_VALID] CHECK ( [isjson]([ExtJSON])> 0)
GO
You can see an extra set of brackets.
Just an idea. Did some ticket get opened? Who knows.
Anyone with questions can contact me directly at wpostma at company dot com, and I work at a company called ramsoft.