SQLite4Unity3d icon indicating copy to clipboard operation
SQLite4Unity3d copied to clipboard

Insert not returning auto increment id

Open rwomack opened this issue 6 years ago • 2 comments

connection.Insert(record) is always returning 1 not the auto incremented id, it appears to be returning RowsAffected from ExecuteNonQuery even though map.HasAutoIncPk is true and it updates the field?

rwomack avatar Jul 22 '19 19:07 rwomack

Yes, you can do a temp fix with renaming the variable long id on line 1374 to count and cast the long into the int and then make the SetAutoIncPK to use the count instead of the long id. Just as bellow:

if (map.HasAutoIncPK)
{
// long id = SQLite3.LastInsertRowid (Handle);
   count = (int) SQLite3.LastInsertRowid (Handle);

// map.SetAutoIncPK (obj, id);
   map.SetAutoIncPK (obj, count);
}

You will then get the auto incremented id

Aresak avatar Oct 06 '19 18:10 Aresak

@Aresak Why introduce messy comments?

int primaryKey = 0;
			
if (map.HasAutoIncPK)
{
    primaryKey = (int)SQLite3.LastInsertRowid (Handle);
    map.SetAutoIncPK (obj, primaryKey);
}

return primaryKey;

danwilkins avatar Sep 07 '21 21:09 danwilkins