Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite: Make AUTOINCREMENT more first-class #10228

Open
Tracked by #22950
smitpatel opened this issue Nov 6, 2017 · 18 comments · Fixed by #22245
Open
Tracked by #22950

SQLite: Make AUTOINCREMENT more first-class #10228

smitpatel opened this issue Nov 6, 2017 · 18 comments · Fixed by #22245
Labels
area-model-building area-sqlite consider-for-next-release priority-bug Issues which requires API breaks and have bigger impact hence should be fixed earlier in the release punted-for-2.1 punted-for-3.0 type-enhancement
Milestone

Comments

@smitpatel
Copy link
Contributor

public partial class Preference
{
    public int PreferenceId { get; set; }
    public string Name { get; set; }
    public int? Value { get; set; }
    public string ValueString { get; set; }
}
modelBuilder.Entity<Preference>(entity =>
{
    entity.HasKey(e => e.PreferenceId);

    entity.Property(e => e.PreferenceId).HasColumnName("PreferenceID");

    entity.Property(e => e.Name).HasMaxLength(50);

    entity.Property(e => e.Value).HasDefaultValueSql(@"((0))");

    entity.Property(e => e.ValueString)
        .HasMaxLength(50)
        .HasDefaultValueSql(@"('')");
});

Generates following migration

migrationBuilder.CreateTable(
    name: "Preference",
    columns: table => new
    {
        PreferenceID = table.Column<int>(nullable: false)
            .Annotation("Sqlite:Autoincrement", true),
        Name = table.Column<string>(maxLength: 50, nullable: true),
        Value = table.Column<int>(nullable: true, defaultValueSql: "((0))")
            .Annotation("Sqlite:Autoincrement", true),
        ValueString = table.Column<string>(maxLength: 50, nullable: true, defaultValueSql: "('')")
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Preference", x => x.PreferenceID);
    });

This is because of ad-hoc logic here
https://github.com/aspnet/EntityFrameworkCore/blob/b86eb8548a0deedc1199c3b4bc6b8632bd7824e3/src/EFCore.Sqlite.Core/Migrations/Internal/SqliteMigrationsAnnotationProvider.cs#L34-L38

And due to other hacks, later all annotations which are not on PK gets ignored. We should make autoincrement a first class for provider just like how SqlServer deals with identity.

@ErikEJ - SqlCE faces the same issue due to similar code and in SQL CE it tries to create multiple Identity columns failing at Update-Database command. You would also need to update SQL CE provider. (I found this after talking to customer on slack who hit issue on SQL CE)

@ajcvickers
Copy link
Contributor

Assigning to @AndriySvyryd to give SQLite the same treatment (flag conventions, etc.) that SQL Server uses for this. @bricelam can help out on the Migrations part, if needed.

@ajcvickers
Copy link
Contributor

@AndriySvyryd I added "propose-close" to make you feel at home with this one. :trollface:

@smitpatel
Copy link
Contributor Author

It utilizes SqlServerValueGenerationStrategy.

@ghost ghost closed this as completed in #22245 Aug 26, 2020
ghost pushed a commit that referenced this issue Aug 26, 2020
Turns out SqlServer:Identity is also not first class. It is computed based on ValueGenerationStrategy and ValueGenerated flag.
Aligned Sqlite to do the same to compute AutoIncrement only when applicable

Resolves #10228
@smitpatel smitpatel reopened this Aug 26, 2020
@bricelam bricelam changed the title SQLite: Make AUTOINCREMENT accurate SQLite: Make AUTOINCREMENT more first-class Aug 27, 2020
@smitpatel smitpatel removed their assignment Aug 27, 2020
@ajcvickers ajcvickers modified the milestones: Backlog, MQ Oct 20, 2021
@bricelam bricelam removed their assignment Jul 8, 2023
@voroninp
Copy link

Also not in v 8? =)

@roji
Copy link
Member

roji commented Aug 24, 2023

@voroninp as always, we prioritize features based on user interest (among other things), and this issue has received only 2 votes.

@voroninp
Copy link

@roji , I keep questioning myself how come I always bump into edge cases while others are happy with what they have =D

@bricelam
Copy link
Contributor

To be fair, at least four other people have ran into issues because of the current design. Unfortunately they, like @voroninp, haven't upvoted this issue. Remember to vote if you want to see things fixed. With 1.9k issues it really can make years of difference.

@voroninp
Copy link

@bricelam, I'd 100% upvote, if I faced the issue before, but it happened on the 2nd day after I used sqlite provider for the first time. =)

@HalinSky
Copy link

4th Anniversary coming in two weeks or so and I just stumbled upon this while using SQLite for in-memory db for unit testing sheesh.

Upvoted, maybe one day.

@bricelam bricelam added the priority-bug Issues which requires API breaks and have bigger impact hence should be fixed earlier in the release label Nov 28, 2023
@bricelam
Copy link
Contributor

+1 This one is important. The current implementation is causing a lot of issues with value converters.

@mohaaron
Copy link

+1,

@Cohote
Copy link

Cohote commented Dec 13, 2024

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building area-sqlite consider-for-next-release priority-bug Issues which requires API breaks and have bigger impact hence should be fixed earlier in the release punted-for-2.1 punted-for-3.0 type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants