Grails property cannot be null error - mysql

My Grails app worked fine. If I remember corectly all I did was to change a contraint on one of my domain classes from blank: true, nullable:true to blank:false, nullable: false. And now when I try to create a new instance of that class I get following error messages:
Property [contract] of class [class com.app.Request] cannot be null
Property [description] of class [class com.app.Request] cannot be null
Property [productline] of class [class com.app.Request] cannot be null
Property [requestType] of class [class com.app.Request] cannot be null
Property [subject] of class [class com.app.Request] cannot be null
And I've provided values for all of these properties.
I'm guessing there is some conflict between gorm and database. Can someone explain to me whats going on and how to fix it.

I had similar problem, had updated constraints to non-null, but my stored data had nulls for non-null fields. At some point Grails tries to load existing data, but fails on validation step.
Try to update your database with some default values (I mean by using plain SQL UPDATE SET x = 'temp' WHERE x IS NULL), make grails clean and restart your app. Should help.

Related

Accessing a NULL related models in yii2

In yii2 view I am accessing property of related model like below
$objPatientModel->physicianUser->diallingCode->phonecode
To explain it :
I have foreign key physician_user in patient table and in patient table I have dialling code (id from another table -- Diallingcodes) and in diallingcodes table I have attribute phonecode .
Now my problem is if in case value is physician_user is NULL then this throws the errors like 'try to get property of non object' which is because $objPatientModel->physicianUser returns NULL instead of empty object .I want to know is there any class or method that can be overridden in yii2 so that above error can be avoided without placing the checks ?
Use ArrayHelper.
\yii\helpers\ArrayHelper::getValue($objPatientModel, 'physicianUser.diallingCode.phonecode', null);
It returns NULL in case value in physician_user is NULL
ArrayHelper api

Spring Boot JSON Serialization

I'm using Spring Boot 1.3.3 and created a REST controller to add a JSON object into Mongo DB collections.
The data to be added from the JSON object will be a subset of information received from the request. So i have created a JSON request object ( DTO ) and an entity object ( model ) to be stored in Mongo collection.
I'm facing an issue now as the JSON request object is populated with default values for integer ( 0 ) and boolean data types ( false ) even if these fields are not populated as part of the request message. I don't want to store these values in the database.
I have added " spring.jackson.serialization-inclusion=non-null " and " spring.jackson.serialization-inclusion=non-default " properties in my application.properties file but still the fields are populated with default values.
Could anyone please help me out in resolving this issue and bypass the default values. NOTE: It works fine for String data type as they would be NULL values by default if not created.
Thanks in advance
String Attributes accept the null value while the primitive attributes have a default value for example 0 is default value for the int attributes.. to avoid having this values Use Integer instead.
Please use this annotation above your fields in bean class with which you facing the problem and tell me your problem is solved.
'#JsonInclude(Include.NON_NULL)'
Thanks

Field 'class' doesn't have a default value

In our app, I have a class describing a person (eye and hair colour, height, etc). Once the form of registering is fulfilled, I click in the submit button, and an exception is thrown:
Class: java.sql.SQLException
Message: Field 'class' doesn't have a default value
I am scratching my head about this, because none of the classes have a field named class, for obvious reasons (it's a reserved word), and in the database there is no column named class, neither.
Any idea why this happens? And how to fix it?
EDIT:
I tried this:
classThrowingExceptionInstance.class=ClassThrowingException
And now it says Cannot set readonly property: class for class ClassThrowingException
It seems that it is MySQL error 1364 - Message: Field '%s' doesn't have a default value.
Check the table which you modify. Are there any fields without default values? Also analyze INSERT and UPDATE statements if they do not post these field values.
To fix this error:
modify table - set DEFAULT values for fields you need
or
pass concrete values to these fields in INSERT/UPDATE statements.

linq to sql string property from non-null column with default

I have a LINQ to SQL class "VoucherRecord" based on a simple table. One property "Note" is a string that represents an nvarchar(255) column, which is non-nullable and has a default value of empty string ('').
If I instantiate a VoucherRecord the initial value of the Note property is null. If I add it using a DataContext's InsertOnSubmit method, I get a SQL error message:
Cannot insert the value NULL into column 'Note', table 'foo.bar.tblVoucher'; column does not allow nulls. INSERT fails.
Why isn't the database default kicking in? What sort of query could bypass the default anyway? How do I view the generated sql for this action?
Thanks for your help!
If you omit the column, the value becomes the database default, but anything you insert is used instead of the default, example:
INSERT INTO MyTable (ID, VoucherRecord) Values(34, NULL) -- Null is used
INSERT INTO MyTable (ID) Values(34) -- Default is used
Picture for example you have a column that defaults to anything but NULL, but you specifically want NULL...for that to ever work, whatever value you specify MUST override the default, even in the case of NULL.
You need to set Auto-Sync to OnInsert, Auto Generated Value to true and Nullable to false for your column to work. See here for a full run-down with explanation on the Linq side.
For viewing the generated SQL, I have to recommend LinqPad

MySql Null DateTime and MS.net are not playing nice

Heres the issue.
I have a table(mySQL) that contains dates and some of them are null.
I know that I can use DateTime? or Nullable to allow nulls, but I'm not sure where to set it.
What I've tried:
I built the class in the dblm. The date propery has the following attributes set:
Nullable : True
Server Data Type : DateTime
Type : NUllable<DateTime>
These settings allow my code to be built.
When I debug it a get this exception thrown:
System.NullReferenceException: Object reference not set to an instance of an object..
If I try to with these attributes:
Nullable : True
Server Data Type : NUllable<DateTime>
Type : NUllable<DateTime>
I get the same exception as above.
Other ways that did not work:
Nullable : True
Server Data Type : DateTime?
Type : NUllable<DateTime>
Nullable : True
Server Data Type : DateTime?
Type : DateTime?
I'm using .net's 3.5 Framework
Thanks
I remember a problem with mysql's ADO.NET driver, where it was not playing nice with nulled date/time fields at all.
Even IsDBNull was throwing exception on a field with null date/time.
It may be still an issue.
Fixed -
It turned out to be an issue with the connection string to connect to the mySQL DB.
Also I didn't know that it was possible to drop a table into the DBML using the Server Explorer. All I needed to do was connect to the database then drop the table onto the DBML.