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.

Id Not auto incrementing as expected

See original GitHub issue

I am using the latest version of LiteDb on a .Net project. A user can fill in a form and create a person. The class for this is;

public class Witness
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string First_Name { get; set; }
        public string Surname { get; set; }
        public string DOB { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string Postcode { get; set; }
        public DateTime Update { get; set; }
        public string URN { get; set; }
        public string Email { get; set; }
        public string HomePhone { get; set; }
        public string WorkPhone { get; set; }
        public string MobilePhone { get; set; }
        public string Occupation { get; set; }
        public string PlaceOfBirth { get; set; }
        public string[] Investigations { get; set; }
        public bool IsActive { get; set; }
    }

The winform calls a method to create the person as follows;

public static int CreateWitness(string Title, string FirstName, string SurName,string DOB,string Address,string Postcode, string Email,
            string Mobile, string Home, string Work, string Occupation, string POB, int Age,string[] Investigations)
        {
           
            //try
            //{
                using (var db = new LiteDatabase(WitnessDb))
                {                  
                    var witnesses = db.GetCollection<Classes.Witness>("Witness");
                    var witness = new Classes.Witness
                    {
                        Title = Title,
                        First_Name = FirstName,
                        Surname = SurName,
                        DOB = DOB,
                        Address = Address,
                        Postcode = Postcode,
                        Email = Email,
                        MobilePhone = Mobile,
                        WorkPhone = Work,
                        HomePhone = Home,
                        Occupation = Occupation,
                        Update = DateTime.UtcNow,
                         URN = SurName + DateTime.UtcNow.ToString("ddMMHHmmssfffffff"),
                        // Investigations = new string[]{""}
                        PlaceOfBirth = POB,
                        Age = Age,
                        Investigations = Investigations
                        
                    };

                    witnesses.Insert(witness);
                    return witness.Id;
                   }

This initially worked fine I think (wasn’t thoroughly tested). However, the user can add and delete people and now it throws an exception for a duplicate Id.

So if I create 3 people (Id’s 1,2 & 3) and then delete person 2 (Id 2) and then try to create a new person - it throws an exception for a duplicate Id (duplicate being 3). I would expect it to create Id 4 and so on.

I have deleted my Db and tried again but no joy. I have tried indexing and shrinking the Db also.

Where am i going wrong?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:28 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
Brown3246commented, Feb 12, 2018

Confirmed the fix appears to work. I employ Shrink when the app opens so that would explain why the problem occurs after an application restart. Many thanks for the quick fix - and for LiteDb!

0reactions
mbdavidcommented, Feb 10, 2018

You can download from master branch and compile (if you have vs2017) - otherwise, I will publish in this holiday a new version on nuget/github release

Read more comments on GitHub >

github_iconTop Results From Across the Web

auto_increment property not working as expected
I want to create a table so that the primary key gets auto incremented and one can simply add entries like so: insert...
Read more >
My auto increment primary key is not automatically ...
Duplicate entry '0' for key 'PRIMARY' This means ID is not incrementing automatically. I am using the following storage procedure. USE user_db; ...
Read more >
How to Work with Auto-Incrementing IDs in SQL
Intro: auto-incrementing fields and primary keys · Create a table with auto-incrementing IDs · Changing increment values · Insert auto-incremented ...
Read more >
What is Auto Increment in SQL and How to Set Up ...
The Auto Increment in SQL is a feature applied to a field to automatically generate and provide a unique value. Learn everything about...
Read more >
3.6.9 Using AUTO_INCREMENT
The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows: CREATE TABLE animals ( id MEDIUMINT NOT NULL AUTO_INCREMENT, ......
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