issue with SingleTable-Inheritance and Enumerations

Using SingleTable-Inheritance I could not provide Enum-Fields for SubClasses.

Example:

type
  [Entity]
  [Inheritance(TInheritanceStrategy.SingleTable)]
  [DiscriminatorColumn('MEDIA_TYPE', TDiscriminatorType.dtString)]
  TMediaFile = class
  private
    [Column('ID', [TColumnProp.Unique, TColumnProp.Required, TColumnProp.DontUpdate])]
    FId: Integer;
 public
    property Id: integer read FId;
  end;

[Enumeration(TEnumMappingType.emString, 'MP3,WAV')]
TSongFormat= (tsfMP3, tsfWAV);

TSong = class(TMediaFile)
  private
    FSongFormat: TSongFormat;
  public
    property SongFormat: TSongFormat read FSongFormat write FSongFormat;
  end;

  [Enumeration(TEnumMappingType.emString, 'MP4,MPEG')]
  TVideoFormat= (tvfMP4, tvfMPEG);

  [Entity]
  [DiscriminatorValue('VIDEO')]
  TVideo = class(TMediaFile)
  private
    FVideoFormat: TVideoFormat;
  public
    property VideoFormat: TVideoFormat read FVideoFormat write FVideoFormat;
  end;

Inserting TVideo will raise NULL-Exception on FSongFormat, while Inserting TSong will raise NULL-Exception on FVideoFormat...

That's by design, all fields in descendant classes of single-table hierarchy should be nullable. It's described in documentation:

https://doc.tmssoftware.com/biz/aurelius/guide/mapping.html#single-table-strategy

The advantage of this strategy is that the database is simple, and performance is optimized, since queries don't need to have too many joins or unions.

One disadvantage is that all columns belonging to child classes must be declared as not required, since they must be null if the row in the table corresponds to a super class.

Well, I just didn't think of combining nullable with enumerations...
Sometimes things are so easy...

Thank you!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.