Sqlite Create table with INT64 Identity column creates the wrong SQL
See original GitHub issueThis is basically the same as #930 and is giving me a problem. I appreciate that Code First isn’t recommended but we like to use Sqlite in memory to write tests around our queries and this is very successful in most cases.
With a table class
public class MyTable
{
[Identity]
[PrimaryKey]
public long Id { get; set; }
}
Running
ctx.CreateTable<MyTable>();
Throws an exception ‘AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY’
The SQL generated is:
CREATE TABLE [MyTable]
(
[Id] BigInt NOT NULL PRIMARY KEY AUTOINCREMENT
)
However the data type needs to be INTEGER not BigInt.
There is a workaround by setting the DBType = “INTEGER” however this shouldn’t be necessary and, in our use case means changing the production code for testing.
I think that the following added to SQLiteSqlBuilder.cs will fix the issue:
protected override void BuildCreateTableFieldType(SqlField field)
{
if (field.IsIdentity && field.IsPrimaryKey && field.DataType == DataType.Int64)
StringBuilder.Append("INTEGER");
else
base.BuildCreateTableFieldType(field);
}
If this looks sensible I can create a test and PR for it
Environment details
linq2db version: 2.9.4 Database Server: Sqlite Database Provider: SqlLiteDataProvider Operating system: Windows 10 .NET Framework: 4.6.1
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Ok I was being dumb. I’ve added the following and it seems to be ok as a workaround
If a proper fix could be added it would be great
Thanks
@MaceWindu, please also check after fix that
FluentMappingBuilder
changes column type.