question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sqlite Create table with INT64 Identity column creates the wrong SQL

See original GitHub issue

This 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.

SqlLite Autoincrement

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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
RoyChasecommented, Feb 21, 2020

Ok I was being dumb. I’ve added the following and it seems to be ok as a workaround

var ms = new MappingSchema();
ms.AddScalarType(typeof(long), DataType.Int32);
connection.AddMappingSchema(ms);

If a proper fix could be added it would be great

Thanks

0reactions
sdanylivcommented, Feb 21, 2020

@MaceWindu, please also check after fix that FluentMappingBuilder changes column type.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Identity column maximum value in SQLite DBs
In the SQLite shell, try: create table bar (id IDENTITY, name VARCHAR); insert into bar (name) values ("John"); select * from bar;.
Read more >
SQLite Forum: Integer becomes blob
Hello, recently I moved all my data to an SQLite database, it is about 17million records and at a random record instead of...
Read more >
Datatypes In SQLite
The following SQL demonstrates how SQLite uses column affinity to do type conversions when values are inserted into a table. CREATE TABLE ......
Read more >
SQLite Autoincrement
Any attempt to use AUTOINCREMENT on a WITHOUT ROWID table or on a column other than the INTEGER PRIMARY KEY column results in...
Read more >
CREATE TABLE
A set of SQL constraints for each table. SQLite supports UNIQUE, NOT NULL, CHECK and FOREIGN KEY constraints. Optionally, a generated column ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found