robertohuertasm / SQLite4Unity3d

SQLite made easy for Unity3d
MIT License
1.27k stars 265 forks source link

Insert not returning auto increment id #101

Closed rwomack closed 2 months ago

rwomack commented 5 years ago

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?

Aresak commented 4 years ago

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

danwilkins commented 2 years ago

@Aresak Why introduce messy comments?

int primaryKey = 0;

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

return primaryKey;