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
7wonders opened this Issue Oct 10, 2013 · 15 comments

Comments

Projects
None yet
@7wonders

7wonders commented Oct 10, 2013

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

This comment has been minimized.

Show comment
Hide comment
@leonardoxh

leonardoxh Oct 11, 2013

Contributor

Can you post your Device model and the related models?

Contributor

leonardoxh commented Oct 11, 2013

Can you post your Device model and the related models?

@7wonders

This comment has been minimized.

Show comment
Hide comment
@7wonders

7wonders Oct 12, 2013

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 :(

7wonders commented Oct 12, 2013

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

This comment has been minimized.

Show comment
Hide comment
@divjotnanda

divjotnanda Mar 3, 2014

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

divjotnanda commented Mar 3, 2014

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

@matthew-reilly

This comment has been minimized.

Show comment
Hide comment
@matthew-reilly

matthew-reilly Apr 8, 2014

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

matthew-reilly commented Apr 8, 2014

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

@davidgoli

This comment has been minimized.

Show comment
Hide comment
@davidgoli

davidgoli Apr 10, 2014

+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.

davidgoli commented Apr 10, 2014

+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

This comment has been minimized.

Show comment
Hide comment
@lamsaitat

lamsaitat Apr 28, 2014

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.

lamsaitat commented Apr 28, 2014

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

This comment has been minimized.

Show comment
Hide comment
@NHamdi

NHamdi 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().

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

This comment has been minimized.

Show comment
Hide comment
@lamsaitat

lamsaitat May 13, 2014

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.

lamsaitat commented May 13, 2014

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

This comment has been minimized.

Show comment
Hide comment
@twobitlabs

twobitlabs May 15, 2014

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

twobitlabs commented May 15, 2014

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

@twobitlabs

This comment has been minimized.

Show comment
Hide comment
@twobitlabs

twobitlabs May 15, 2014

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.

twobitlabs commented May 15, 2014

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

This comment has been minimized.

Show comment
Hide comment
@fcgregorio

fcgregorio Jun 5, 2014

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.

fcgregorio commented Jun 5, 2014

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

This comment has been minimized.

Show comment
Hide comment
@twobitlabs

twobitlabs Jun 5, 2014

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

twobitlabs commented Jun 5, 2014

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

@lamsaitat

This comment has been minimized.

Show comment
Hide comment
@lamsaitat

lamsaitat Jun 5, 2014

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)
.

lamsaitat commented Jun 5, 2014

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

This comment has been minimized.

Show comment
Hide comment
@schlamar

schlamar Oct 15, 2014

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

schlamar commented Oct 15, 2014

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

This comment has been minimized.

Show comment
Hide comment
@ImMathanR

ImMathanR Nov 4, 2015

Anyone figured out a way around this?

ImMathanR commented Nov 4, 2015

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