Vici coolstorage bug in saving ManyToMany pure relations?

94 views Asked by At

We have a test case that creates a many-many relation between entities X and Y and then deletes the same relation.

SQLite throws a constraint violation that the relation exists. After digging a bit, we found that the relationship table still has an entry for the relationship.

It appears that this may be a bug in Vici CoolStorage itself. The code that handles removed and added objects looks like this (in CSListGeneric.cs):

            if (Relation != null && Relation.PureManyToMany)
            {
                if (_removedObjects != null)
                {
                    foreach (TObjectType obj in _removedObjects)
                    {
                        CSParameterCollection parameters = new CSParameterCollection();

                        parameters.Add("@LocalKey").Value = RelationObject.Data["#" + Relation.LocalKey].Value;
                        parameters.Add("@ForeignKey").Value = obj.Data["#" + Relation.ForeignKey].Value;

                        string deleteSql = DB.BuildDeleteSQL(Relation.LinkTable, null, DB.QuoteField(Relation.LocalLinkKey) + "=@LocalKey and " + DB.QuoteField(Relation.ForeignLinkKey) + "=@ForeignKey");

                        DB.ExecuteNonQuery(deleteSql, parameters);
                    }

                    _removedObjects = null;
                }

                if (_addedObjects != null)
                {
                    foreach (TObjectType obj in _addedObjects)
                    {
                        CSParameterCollection parameters = new CSParameterCollection();

                        parameters.Add("@LocalKey").Value = RelationObject.Data["#" + Relation.LocalKey].Value;
                        parameters.Add("@ForeignKey").Value = obj.Data["#" + Relation.ForeignKey].Value;

                        DB.ExecuteInsert(Relation.LinkTable,
                                            new[] { Relation.LocalLinkKey, Relation.ForeignLinkKey },
                                            new[] { "@LocalKey", "@ForeignKey" },
                                            null, null, null, parameters);

                    }

                    _addedObjects = null;  // THIS WAS ADDED TO FIX THE BUG
                }

Note that the removed objects collection is set to null after the delete statements are executed. However, the added objects collection is not similarly reset.

Adding the line at the bottom appears to fix the issue.

Is this the correct fix, or are we missing something?

0

There are 0 answers