spine-tools / SpineInterface.jl

A package to interact with Spine databases from a Julia session
http://www.spine-model.org
GNU General Public License v3.0
9 stars 5 forks source link

`SpineInterface` can create duplicate `Object`s that causes some confusion. #81

Open Tasqu opened 2 years ago

Tasqu commented 2 years ago

Essentially, it seems that the Object constructor creates a duplicate Object, that fools some of the code into thinking that the Object is the same as one already in the database, while other functionality recognises that there's a difference. For example, when loading a database into memory with a building_archetype ObjectClass with an Object called IDA_ESBO_AB:

julia> ba1 = building_archetype(:IDA_ESBO_AB)
julia> ba2 = Object(:IDA_ESBO_AB, :building_archetype)
julia> ba1 == ba2
julia> true

but

julia> ba1 === ba2
julia> false

Furthermore, if comparing the fields of the Objects:

julia> [getfield(ba, field) for field in fieldnames(typeof(ba))] .== [getfield(ba2, field) for field in fieldnames(typeof(ba2))]
5-element BitVector:
 1
 1
 0
 1
 1

and

julia> [getfield(ba, field) for field in fieldnames(typeof(ba))] .=== [getfield(ba2, field) for field in fieldnames(typeof(ba2))]
5-element BitVector:
 1
 1
 0
 0
 1

where

julia> fieldnames(typeof(ba))
(:name, :class_name, :members, :groups, :id)

I believe that spinedb_api doesn't allow duplicate objects inside the same object class, so we should probably fix the Object constructor so that it either throws an error when trying to create a duplicate object, or refers to the pre-existing object instead.

Tasqu commented 2 years ago

Well, it turns out that this is a pretty fundamental problem. The easiest way would be to simply prevent users from creating Objects that already exist, e.g. by checking their database ids. Otherwise, we start running into issues about what the user tries to do with the Object constructor. However, I'm not sure a change like this wouldn't mess something up.

E.g. how should the Object constructor handle a situation where there already exists an object with a given name, class_name, members, groups, and id, and the user uses the Object constructor to create a new object with the exact same name and class_name, but different members and groups? Should SpineInterface interpret this as an attempt to update the existing Object, or an attempt to create a new Object? And if the name and object_class are identical, how are the Objects distinguished from one another?