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

Allow saving a model again after deleting it #369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.orm.SugarRecord.save;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -95,6 +96,18 @@ public void deleteTest() throws Exception {
assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class));
}

@Test
public void deleteAndSaveTest() throws Exception {
SimpleExtendedModel model = new SimpleExtendedModel();
long oldId = model.save();
model.delete();
long newId = model.save();

assertNotEquals(oldId, -1L);
assertNotEquals(newId, -1L);
assertNotEquals(oldId, newId);
}

@Test
public void deleteUnsavedTest() throws Exception {
SimpleExtendedModel model = new SimpleExtendedModel();
Expand Down
4 changes: 3 additions & 1 deletion library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ public boolean delete() {
if (id != null && id > 0L) {
SQLiteDatabase db = getSugarContext().getSugarDb().getDB();
Log.i("Sugar", type.getSimpleName() + " deleted : " + id);
return db.delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1;
boolean deleted = db.delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1;
if (deleted) setId(null);
return deleted;
} else {
Log.i("Sugar", "Cannot delete object: " + type.getSimpleName() + " - object has not been saved");
return false;
Expand Down