question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deleting stubbed foreign key only works through model.delete()

See original GitHub issue

DBFlow Version: 4.0.4 Issue Kind (Bug, Question, Feature): Bug

Description: Currently, after setting up everything correctly,

  1. Enabling foreign key constraints
@Database(
    name = OrgaDatabase.NAME,
    version = OrgaDatabase.VERSION,
    insertConflict = ConflictAction.IGNORE,
    updateConflict= ConflictAction.REPLACE,
    foreignKeyConstraintsEnforced = true
)
  1. Defining all correct annotations to the foreign key in model
@ForeignKey(
    stubbedRelationship = true,
    saveForeignKeyModel = true,
    deleteForeignKeyModel = true,
    onDelete = ForeignKeyAction.CASCADE)
@SerializedName("user_detail")
private UserDetail userDetail;
  1. Defining a primary key in foreign model
@Table(database = OrgaDatabase.class, allFields = true)
public class UserDetail extends BaseModel {

    @PrimaryKey(autoincrement = true)
    private int id;

    private String contact;
    private String twitter;
    @SerializedName("firstname")
    private String firstName;
    private String avatar;
    private String facebook;
    @SerializedName("lastname")
    private String lastName;
    private String details;
}

I’m only able to delete the foreign key model from database by calling user.delete()

Both of these methods don’t work ->

  1. Delete.table(User.class);
  2. SQLite.delete().from(User.class).execute()

I have to resort to this to actually delete the user and userdetails

Click to see code

User user = new User();
UserDetail userDetail = new UserDetail();
user.setUserDetail(userDetail);
user.setId(2);
user.setEmail("jamal.areeb@gmail.com");
user.setLastAccessTime("lol");
user.setSignupTime("bawa");
userDetail.setFirstName("Areeb");
userDetail.setLastName("Jamal");
userDetail.setDetails("A B  C D");

user.save();

databaseRepository.getAllItems(UserDetail.class).subscribe(user1 -> {
    System.out.println("Detail : " + user1);
});

List<User> users =  SQLite.select().from(User.class).queryList();

for (User user1 : users) {
    user.delete();
}

databaseRepository.getAllItems(UserDetail.class).subscribe(user1 -> {
    System.out.println("Detail : " + user1);
});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
agrosnercommented, Jul 14, 2017

you can use FlowManager.getModelAdapter(User.class).deleteAll(models)

0reactions
iamareebjamalcommented, Jul 14, 2017

OK 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem in Deleting Model Entries having ForeignKey ...
I tried implementing your problem on my localmachine. Django causes this error because of foreign key constraint.
Read more >
Django ORM | Foreign Key Deletion Constraints - YouTube
In this tutorial we review the 7 behaviours that we can define for when the referenced object is deleted in a foreign key...
Read more >
Cascade Delete - EF Core | Microsoft Learn
Configuring cascading behaviors triggered when an entity is deleted or severed from its principal/parent.
Read more >
SQL Server: Foreign Keys with set null on delete
A foreign key with "set null on delete" means that if a record in the parent table is deleted, then the corresponding records...
Read more >
CHANGES — oslo.versionedobjects 1.18.0 documentation
Update README.rst; Remove serialize_args(), per the work items ... Remove instance Foreign Key in volumes table, replace with instance_uuid; Remove old ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found