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

SQLiteConstraintException #127

Open
ScottEAdams opened this issue Oct 10, 2013 · 15 comments
Open

SQLiteConstraintException #127

ScottEAdams opened this issue Oct 10, 2013 · 15 comments

Comments

@ScottEAdams
Copy link

When doing

SQLiteUtils.execSql("DELETE FROM Devices");

or

new Delete().from(Device.class).execute();

It works fine some of the time but then out of nowhere it throws

android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
            at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
            at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:858)
            at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
            at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
            at com.activeandroid.util.SQLiteUtils.execSql(Unknown Source)

I have a foreign key on the table that I have tried setting

onDelete = Column.ForeignKeyAction.SET_NULL

and

onDelete = Column.ForeignKeyAction.CASCADE

and without any onDelete and the same problem arises. Any ideas?

@leonardoxh
Copy link
Contributor

Can you post your Device model and the related models?

@ScottEAdams
Copy link
Author

It seemed to be happening on all devices I tried it on -
Sony Ericsson LT18i (Phone) Android 4.0.4
Semc Xperia Arc S (Phone) Android 4.2.2
Samsung GT-I9100 (Phone) Android 4.1.2
HTC Wildfire S A510e (Phone) Android 2.3.5

@Table(name = "Devices")
public class Device extends Model
{
    @Column(name = "device_id", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    public int device_id;

    @Column(name = "resource_uri")
    public String resource_uri;

    @Column(name = "vehicle", onDelete = Column.ForeignKeyAction.SET_NULL) <- have tried this with CASCADE also
    public Vehicle vehicle;
}

@Table(name = "Vehicles")
public class Vehicle extends Model
{
    @Column(name = "vehicle_id", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    public int vehicle_id;

    @Column(name = "resource_uri")
    public String resource_uri;
}

I got so frustrated with it that I ended up removing all relations :(

@divjotnanda
Copy link

Facing the same issue with my application also.
Can anyone help...?

@matthew-reilly
Copy link

This still seems to be an issue. If anyone figured this out, some help would be appreciated.

@davidgoli
Copy link

+1 I'm seeing this issue as well, exactly as reported. It seems after deleting the app from the device, the first install is fine, but subsequent installs consistently fail. This is a major bug.

@lamsaitat
Copy link

Encountering the exact same problem as @davidgoli . Have added onDelete = Column.ForeignKeyAction.CASCADE as others have suggested from google search but didn't resolve the problem.

@NHamdi
Copy link

NHamdi commented May 13, 2014

Encountered this problem today, if anyone still have the "issue", you just have to delete all relations before, in this case, you have to call Vehicles.deleteall(), before Devices.deleteAll().

@lamsaitat
Copy link

I managed to solve my problem at the end. With my case it was at update that caused the exception. Adding onUpdate = CASCADE solved it.

@twobitlabs
Copy link

+1 I'm seeing this problem too. Deleting the dependent records first resolves it, adding onDelete = Column.ForeignKeyAction.CASCADE does not.

@twobitlabs
Copy link

Browsing through the ActiveAndroid code, it appears that the onDelete annotation is only read when the column is created. If the column already exists in your installation, the ON DELETE action will not be added. You have to create a migration to add it.

@fcgregorio
Copy link

Just to be sure, is onDelete = Column.ForeignKeyAction.CASCADE already working even with uninstalling then installing app? I'm having difficulty making it work. Right now I'm just deleting the dependent records first then I delete the referenced records.

@twobitlabs
Copy link

Yes, for me it worked to uninstall the app, as the tables get re-created with the ON DELETE action on the column.

@lamsaitat
Copy link

yes you do need to purge existing table (or update the table with the right
migration statement) because on delete and on update is part of the schema.
On 06/06/2014 1:48 am, "Two Bit Labs" notifications@github.com wrote:

Yes, for me it worked to uninstall the app, as the tables get re-created
with the ON DELETE action on the column.


Reply to this email directly or view it on GitHub
#127 (comment)
.

@schlamar
Copy link

Just stumbled about this issue per accident. There is a bug in SQLite regarding Foreign Keys which occurs in 3.6.21 and is fixed at least since 3.8.1. This might be the cause of this bug. More details can be found here: http://bugs.python.org/issue19450

@ImMathanR
Copy link

Anyone figured out a way around this?

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

No branches or pull requests