RushOrm icon indicating copy to clipboard operation
RushOrm copied to clipboard

Saving wont upgrade, but will create a new table instead

Open AvenaIO opened this issue 9 years ago • 4 comments

Hi, I have this:

public class UserData extends RushObject {
    private String name;
    private String age;
    private String gender;
    private String location;
    private String weight;
    private String height;
    private String health_issue;
    private String marital_status;
    private String current_medication;
    private String physical_activity;
    private String food_dislikes;
    private String allergies;
    private String smoking_habits;
    private String drinking_habits;
    private String family_health_history;

    /* Classes must include an empty constructer */
    public UserData() {}
...

My issue is, if I set the value of a single column, all columns will be created, which is fine per se. But, once I try to set another value, it will create a new table for just that one single value.

How can I fix this? Thank you.

AvenaIO avatar Feb 11 '16 22:02 AvenaIO

Hi,

I don't quite understand whats going on are you able to copy in the setting and saving code?

Thanks

Stuart-campbell avatar Feb 12 '16 10:02 Stuart-campbell

are you able to copy in the setting and saving code?

I don't quite understand what do you mean by this, but I am able to save and read from the db.

My problem is, if I have a table with 3 columns, and when I save and only fill 1 column, the other 2 will be saved null, and when I try to save those other two, it will create new tables for those columns, instead of filling the null columns from the first table.

AvenaIO avatar Feb 13 '16 16:02 AvenaIO

It sounds like you have slightly misunderstood how the ORM works.

There is 1 table per class. Every time you save an object it creates a new row in that table.

So as you are saving two different objects you have two rows. This is the correct behaviour.

It sounds like what you want is to update an exciting object. To do this you must first load it from the database then update the fields then save.

Example:

First time.

UserData userdata = new UserData();
userdata.setStuff();
userdate.save();

Update.

UserData userdata = new RushSearch().findSingle(UserData.class);
userdata.setOtherStuff();
userdate.save();

Or always do this.

UserData userdata = new RushSearch().findSingle(UserData.class);
if(userdata == null) {
    userdata = new UserData();
}
userdata.setStuff();
userdate.save();

Hope that helps Thanks

ghost avatar Feb 13 '16 16:02 ghost

Cheers! I did not know how to update the rows. Thanks a lot, I will try this asap.

AvenaIO avatar Feb 13 '16 16:02 AvenaIO