ivoa / vo-dml

VO-DML tooling and documents.
Creative Commons Attribution Share Alike 4.0 International
1 stars 1 forks source link

way to indicate "natural" primary key #4

Open pahjbo opened 2 years ago

pahjbo commented 2 years ago

When generating code for saving models in databases, it is sometimes the case that one (or more) of the existing attributes makes a good primary key for the particular ObjectType, and in these cases it would be good to have a way in VO-DML of indicating which one(s) to use rather than creating a generated artificial key. The ways that I can think of doing this are

  1. Use constraints to specify that an attribute should be unique (or perhaps more specifically a key).
  2. Have a special primitive type - e.g. identifier (with perhaps sub-types IntegerIdentifier and StringIdentifier) to indicate suitable attributes.

I can see that there are pros and cons of both approaches - the first feels more proper, but requires an extension to the VO-DML meta-model, whereas the second can be done with just defining some primitives, but then requires a "convention" to exist (and it would not be as easy to specify compound key order).

pahjbo commented 2 years ago

Have thought about it a bit more and I think that option 1 above is the best, as the new VO-DML schema would be backwards compatible, as it would just be adding a new construct - I am trialing this

    <xsd:complexType name="NaturalKey">
       <xsd:annotation>
          <xsd:documentation>
             This constraint is used to indicate that an attribute is a natural key for its owning ObjectType, meaning that the
             attribute value should be globally unique. This may be applied multiple times to indicate that only a composition
             of several attributes make the globally unique key.
          </xsd:documentation>
       </xsd:annotation>
       <xsd:complexContent>
          <xsd:extension base="Constraint">
             <xsd:sequence>
                <xsd:element name="Position" type="xsd:positiveInteger">
                        <xsd:annotation>
                           <xsd:documentation>In the case where multiple attribute values make up the natural key, this
                           value indicates the ordinal number of this particular key in the compound key.</xsd:documentation>
                        </xsd:annotation>
                </xsd:element>
             </xsd:sequence>
          </xsd:extension>
       </xsd:complexContent>
    </xsd:complexType>

as an aid to creating nicer Java code (with better XML and SQL schema too)

pahjbo commented 2 years ago

this extension is now being trialed by the latest release of the gradle tooling.