Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Error when updating objects #7

Closed
nis73 opened this issue Feb 19, 2015 · 11 comments
Closed

SQL Error when updating objects #7

nis73 opened this issue Feb 19, 2015 · 11 comments
Assignees
Labels

Comments

@nis73
Copy link

nis73 commented Feb 19, 2015

First, thanks for your great work. I'm planning to use your framework for my next app.
I'm having the following issue: When I create objects with parent-child relationship, store them, load them again, change something and store them again the framework crashes with a SQL Error. See the following minimal working example:

public class Child extends RushObject {

    private String string;

    public Child(String string) {
        this.string = string;
    }

    public Child() {
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }
}

public class Parent extends RushObject {

    @RushList(classname = "de.test.testrushorm.Child")
    private List<Child> children;

    public Parent() {
        this.children = new ArrayList<Child>();
    }

    public boolean add(Child child) {
        return children.add(child);
    }

    public boolean remove(Child child) {
        return children.remove(child);
    }

    public List<Child> getChildren() {
        return children;
    }
}

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Parent parent = new Parent();
        parent.save();

        Child firstChild = new Child();
        firstChild.save();

        Child secondChild = new Child();
        secondChild.save();

        parent.add(firstChild);
        parent.add(secondChild);
        parent.save();

        parent = new RushSearch().find(Parent.class).get(0);
        parent.getChildren().get(0).setString("test1");
        parent.save();

    }
}

Stacktrace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.testrushorm/de.test.testrushorm.MainActivity}: co.uk.rushorm.core.exceptions.RushSqlException: This is most likely caused by a change in data structure or new RushObject.
This issue should be resolved by a database migration.
This can be done by updating db version number in the manifest or setting Rush in debug mode.
To set Rush in debug mode add this to your manifest file -
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2103)
at android.app.ActivityThread.access$600(ActivityThread.java:137)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4827)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: co.uk.rushorm.core.exceptions.RushSqlException: This is most likely caused by a change in data structure or new RushObject.
This issue should be resolved by a database migration.
This can be done by updating db version number in the manifest or setting Rush in debug mode.
To set Rush in debug mode add this to your manifest file -
at co.uk.rushorm.android.AndroidRushStatementRunner.runRaw(AndroidRushStatementRunner.java:35)
at co.uk.rushorm.core.RushCore$6.createdOrUpdateStatement(RushCore.java:238)
at co.uk.rushorm.core.implementation.ReflectionStatementGenerator$3.doAction(ReflectionStatementGenerator.java:366)
at co.uk.rushorm.core.implementation.ReflectionStatementGenerator.doLoop(ReflectionStatementGenerator.java:73)
at co.uk.rushorm.core.implementation.ReflectionStatementGenerator.updateObjects(ReflectionStatementGenerator.java:342)
at co.uk.rushorm.core.implementation.ReflectionStatementGenerator.generateSaveOrUpdate(ReflectionStatementGenerator.java:169)
at co.uk.rushorm.core.RushCore.save(RushCore.java:224)
at co.uk.rushorm.core.RushCore.save(RushCore.java:95)
at co.uk.rushorm.core.RushCore.save(RushCore.java:90)
at co.uk.rushorm.core.RushObject.save(RushObject.java:10)
at de.test.testrushorm.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5013)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2042)
... 11 more

The error seems to be related to the creation of the sql update statements. Any ideas? Did I do something wrong?

@Stuart-campbell
Copy link
Owner

Can you confirm you have this set to true in your manifest?

<meta-data android:name="Rush_debug" android:value="true" />

Also as a side note, you do not need to save the children first, saving the parent object takes care of that for you.

@Stuart-campbell Stuart-campbell self-assigned this Feb 19, 2015
@nis73
Copy link
Author

nis73 commented Feb 19, 2015

Thanks for the hint.
Here's my manifest:

<application
    android:name=".TestApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <!-- Updating this will cause a database upgrade -->
    <meta-data android:name="Rush_db_version" android:value="3" />
    <!-- Database name -->
    <meta-data android:name="Rush_db_name" android:value="rush.db" />
    <!-- Setting this to true will cause a migration to happen every launch,
    this is very handy during development although could cause data loss -->
    <meta-data android:name="Rush_debug" android:value="true" />
    <!-- Setting this to true mean that tables will only be created of classes that
    extend RushObject and are annotated with @RushTableAnnotation -->
    <meta-data android:name="Rush_requires_table_annotation" android:value="false" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

@Stuart-campbell
Copy link
Owner

Ok, can I also confirm you are using v0.8.0? Obstructing the underlying SQL error was a mistake which ill be updating soon.

@nis73
Copy link
Author

nis73 commented Feb 19, 2015

Yes, I used v0.8.0 with this example. Yesterday I was using v0.7.0 and got a more expressive log output, stating an invalid sql update statement of the form

UPDATE <Table> SET field="value1" WHERE id=1, SET field="value2" WHERE id=2

The UPDATE keyword was missing for the second object.

@Stuart-campbell
Copy link
Owner

If you add just a random extra field to the parent does that fix it?

@nis73
Copy link
Author

nis73 commented Feb 19, 2015

I added an extra String field to the parent, but unfortunately it still crashes with the same exception

@Stuart-campbell
Copy link
Owner

Are you classes inner classes?

@nis73
Copy link
Author

nis73 commented Feb 19, 2015

No, these are normal classes

@Stuart-campbell
Copy link
Owner

I have reproduced your crash, I think I already have a fix on another branch which is basically ready to integrate so should be fixed very soon.

Thanks for you patience.

@Stuart-campbell
Copy link
Owner

Should be fixed now with version 1.0.0, will require a clean install though. Let me know if it works.

Thanks

@nis73
Copy link
Author

nis73 commented Feb 20, 2015

Thanks a lot for your quick fix. It works now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants