Unable to save object - invalid data type for properties.

Recently I have added in my Backendless table new column with Integer type.
But I get the exemption: Unable to save object - invalid data type for properties -<field name>. You can change the property type in developer console.

If I change the column type to DOUBLE, it work fine! But I have INTEGER type in my java object for this field!

I use the latest SDK.

Hi Timofey,

I tried to reproduce the problem and it worked for me. Here’s what I did:

Created class Foo:

package com.backendless.sample;

public class Foo
{
  public int intField;
}

Create code that creates an instance of Foo and saves it in Backendless:

  public static void testInt()
  {
    Foo foo = new Foo();
    foo.intField = 22;
    Backendless.Data.of( Foo.class ).save( foo );
    System.out.println( "foo is saved" );
  }

Once the object was saved, I checked it in Backendless console - see two attached screenshots.

How is it different from your code? How recent is the Backendless SDK you use?

Regards,
Mark

Hi Mark,

My class:


public class PeopleLikeBaaS {


    private Integer peopleLikeType;
    private Integer peopleLikeAnswerType;
    private Integer timerStartedType; //  Unable to save object - invalid data type for properties
    private Date updated;
    private String toUser;
    private String fromUser;
    private String objectId;
    private Date created;
    private String ownerId;
}
PeopleLikeBaaS peopleLikeBaaS = Backendless.Data.of(PeopleLikeBaaS.class).save(mPeopleLikeBaaS);

Please find the screenshot at the attachment.

When I save it I got error.
if I change type of “timerStartedType” to DOUBLE in Backendless table everything is OK.

I use Backendles several month and I did not have this error before.

Hi Timofey,

Thanks for sharing the code. I assume you have getters and setters for each private field in your class?

Regards,
Mark

Yes, I have getters and setters.

It seems I found the cause of the problem This happens when I have empty value.
If I set 0 for “timerStartedType” it works.

public class PeopleLikeBaaS {



private Integer peopleLikeType;
private Integer peopleLikeAnswerType;
private Integer timerStartedType = 0; // Unable to save object - invalid data type for properties
private Date updated;

I see, so to narrow it down, an unitialized int value in a complex type causes an issue. We’ll be looking into it.

Thanks!

Mark

Hi
I have the same problem, in my table I have an integer value, if I do not put a value to that integer, then I can’t save the object.
Is a requirement initialize all integers values?

Thanks.

Mark,

Is the problem fixed on your side? Any SDK updates?

Timofey,

I just pushed a change into the repository which will handle this use-case. Uninitialized Integer and Double objects will be persisted as 0 values. I apologize it took us so long to do it. As soon as all the tests go through, we will package the SDK and update the website with it.

Regards,
Mark

Hi all,I noticed that I also have this problem,but my Integer value has already initialized.

Can you elaborate? For example the following would make it so much easier for us to help you:

    Sample code Description of the problem Specific error you getting What SDK you use (or REST).
Mark

hi Mark,

similarly to what described above, when I upload an entity with fields that are not initialized they get uploaded as integers = 0. However these fields are actually double (they are declared as Numeric in their class) and therefore when I upload an entity later on, where the fields are used and in double format, it won’t upload them because of a type mismatch. Suggestions? Thanks

Hi Marco,

When you say “upload” I assume you’re using the API to save/update an object. What error do you get? Can you show the class you’re saving and the schema in Backendless for the table corresponding to the class?

Regards,
Mark

hi Mark,

yes I am referring to e.g.

[[backendless.persistenceService of:[MY_CLASS class]] save:instance_of_my_class 

and I get the same error indicated above:

“But I get the exemption: Unable to save object - invalid data type for properties -<field name>. You can change the property type in developer console.”

I’ll elaborate a bit.
Say I have a class in Objective-C with some fields that are Numeric (some of them are integers and some of them are double, but for the class everything is Numeric, right?).

Now, say that I create one of these objects, and I do not set a value for these numeric fields. At the first upload of the first object (calling save, when the database is still completely empty and doesn’t know the class), the object will get online and a schema will be dynamically defined. So based on the object properties (I’m assuming) you determine if you should have strings, integers or other in the schema.

However for the object fields that I did not set, and are Numeric properties, the schema decides that those fields are of type INT. However those fields are not necessarily of type INT, as I wrote above they could be of type double. So the next time when instead of keeping the field empty I populate it, I cannot write the object (i.e. the save function returns the error above), because the schema thinks the field is an INT while it’s actually a double and I’m trying to send a double.

Is this clear? Basically the dynamic schema creation assumes Numerics are INT, but they might be double, and if you do not write any value in some attributes, this causes problems in successive calls to save.

My workaround to this is the following now: I first upload ONE object with ALL fields set (like a nonsense object that is used just to create the schema properly, by setting double where double will be later on), and then I can proceed with the regular logic of my app. This is not ideal but works, because since in the first object all fields are set, there is no risk that the schema misunderstands types.

I also tried to change type on the schema online since it can be edited, but when I set them to double then they go back to int, so for now I will stick to the workaround unless you have other ideas. The problem comes from the fact that I have a lot of non-compulsory fields in these objects.

Hi Marco,

That makes sense now. The algorithm on the server side to determine what type to assign to a column uses values received by the server and not the declaration in your class. When it comes to the numeric values, if it is in the range of INT, the type will be assigned as INT. If it is outside of the int range, it will be double.

Have you tried saving a double value (meaning a real double number, rather than an int which is stored in a double property)?

Regards,
Mark

I think the problem comes from the fact that by default all values of the objective-c object are zero so unless I set it to some decimal number (which is what I do now with the initial “fake” object), it will always set it as INT. No big deal

I do not think there is a problem, as long as you do not lose data and everything is stored/retrieved you intend to, things are good. The logic is to update the data type of the column in the database based on the largest value that is present in any of the objects.

mh but if I write a double and the column is already set as INT, the upload fails.

What error do you get in that case?