Skip to content

Child object temporary added, then, removed, but gets inserted anyway #2028

Open
@rverberne

Description

@rverberne

In a simple parent -> child relationship when adding a child to a existing parent, then doing a query, then removing the child, the child gets inserted in the database as a orphan.

It can cause SQL errors later on, other transactions/constraints dont expect the orphans.

Is this expected behaviour or a bug?

using (var transaction = session.BeginTransaction())
{
    Parent parent = session.Get<Parent>("1");

    var child = new Child();
    child.Id = "99";
    child.ChildName = "Child 99";

    parent.Childs.Add(child);

    // Auto flush? Inserts the child record?
    Settings settings = session
        .QueryOver<Settings>()
        .Take(1)
        .SingleOrDefault();

    // Remove child from parent, child is no longer referenced
    parent.Childs.Remove(child);

    // Child record is not deleted here,
    // parent_id column will be NULL.
    session.Update(parent);
    transaction.Commit();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions