praeclarum / sqlite-net

Simple, powerful, cross-platform SQLite client and ORM for .NET
MIT License
4.08k stars 1.42k forks source link

Autoincrement rows should only autoincrement if no value is specified #235

Open jacksonh opened 11 years ago

jacksonh commented 11 years ago

According to sqlite docs:

When a new row is inserted into an SQLite table, the ROWID can either be specified 
as part of the INSERT statement or it can be assigned automatically by the database 
engine. To specify a ROWID manually, just include it in the list of values to be inserted.

If no ROWID is specified on the insert, or if the specified ROWID has a value of NULL, 
then an appropriate ROWID is created automatically.

sqlite-net ignores autoincrement primarykey columns when inserting, so its difficult to replicate this behavior.

jacksonh commented 11 years ago

My proposal for implementing this is to add a new attribute AutoIncrementIfNotNull that can only be added to properties of Object or Nullable type. Columns with this attribute will still be included in the insert columns list, and if they are null they will get autoincremented. If the columns have a non-null value, that value will be inserted.

sqlite does most of this for us, all we need to do is make sure these columns are in the list of mapped columns.

enantiomer2000 commented 6 years ago

Wow. Can't believe this issue is still open. I followed @jacksonh suggestion to deal with the open issue.

ish-1313 commented 3 years ago

its a little bit old issue, but hit it today implementing some migrations. started to code manual sql commands, but ends up with simple two pass code first - create dbrow with only required fields with correct rowid, and then just update them

foreach (var recipe in oldRecipes)  
{
      dbConnection.Execute($"insert into Recipes (ID) Values ({recipe.ID})");
}
dbConnection.UpdateAll(oldRecipes, false);

may be it helps somebody