mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.61k stars 1.25k forks source link

[BUG] Unexpected about autoId after delete in V5 #1856

Open yaozhong-OMEC opened 4 years ago

yaozhong-OMEC commented 4 years ago

I am using LiteDB 5.0.9. I found the autoid will didn't work well after delete.

I read the source code and found a unit test about autoid after delete is AutoId_No_Duplicate_After_Delete(). But I think people will not often add data immediately after delete . So I change test like this, it wrong.

        [Fact]
        public void AutoId_No_Duplicate_After_Delete1()
        {
            // using strong type
            using (var db = new LiteDatabase("database.db"))
            {
                var col = db.GetCollection<EntityInt>("col1");

                var one = new EntityInt { Name = "One" };
                var two = new EntityInt { Name = "Two" };

                // insert
                col.Insert(one);
                col.Insert(two);

                one.Id.Should().Be(1);
                two.Id.Should().Be(2);

                // now delete first 2 rows
                col.Delete(one.Id);
                col.Delete(two.Id);
            }

            using (var db = new LiteDatabase("database.db"))
            {
                var col = db.GetCollection<EntityInt>("col1");

                var three = new EntityInt { Name = "Three" };
                var four = new EntityInt { Name = "Four" };

                // and insert new documents
                col.Insert(new EntityInt[] { three, four });

                three.Id.Should().Be(3);
                four.Id.Should().Be(4);
            }
        }

The error message is Expected three.Id to be 3, but found 1..

fraenke75 commented 3 years ago

I found this issue in a real live situation. It would be great if this could be fixed and that the auto increment is not decreased after a delete operation.