chennaione / sugar

Insanely easy way to work with Android Database.
http://satyan.github.com/sugar/
MIT License
2.62k stars 583 forks source link

#update method #id #@Unique #633

Open Shellton opened 8 years ago

Shellton commented 8 years ago

The thing is that when i call update() method, in memory , all item turn to same one. could you tell me that why how to update one row when user have not specify @Unique 。

fg607 commented 8 years ago

@Shellton I have the same situation, as i know that SugarRecord has a private field "id" which is unique,so the update() method should update the item which is marked by its id. So, that's confused.

fg607 commented 8 years ago

Segment of update() method source code:

for (Field column : columns) { if(column.isAnnotationPresent(Unique.class)) { try { column.setAccessible(true); String columnName = NamingHelper.toSQLName(column); Object columnValue = column.get(object);

                whereClause.append(columnName).append(" = ?");
                whereArgs.add(String.valueOf(columnValue));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            if (!column.getName().equals("id")) {
                ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap);
            }
        }
    }

    String[] whereArgsArray = whereArgs.toArray(new String[whereArgs.size()]);
    // Get SugarRecord based on Unique values
    long rowsEffected = db.update(NamingHelper.toSQLName(object.getClass()), values, whereClause.toString(), whereArgsArray);

    if (rowsEffected == 0) {
        return save(db, object);
    } else {
        return rowsEffected;
    }

The source code shows that update() method do not use the "id" field as the condition of update items.