drift icon indicating copy to clipboard operation
drift copied to clipboard

Polymorphic table references

Open Meryldominguez opened this issue 1 year ago • 1 comments

Hello all! Apologies if this solution exists, I am rather new to flutter/dart/drift and still learning a ton.

I am trying to implement a polymorphic Tags table, and cannot find anything in the docs to suggest that is possible.

Ideally It could look something like this?

@TableIndex(name: 'tags_name', columns: {#name})
class Tags extends Table with Timestamps, AutoIncrementingPrimaryKey{

  TextColumn get name => text().withLength(min: 6, max: 32)();
}

class TagItem extends Table with Timestamps, AutoIncrementingPrimaryKey{
  IntColumn get tagId =>
      integer().nullable().references(Tags, #id)();
  IntColumn get itemId =>
      integer().nullable().references([Composers, People, Events], [#id, #id, #id])();
}

Is this in the works? already supported?

Meryldominguez avatar Nov 28 '24 01:11 Meryldominguez

There is no support for polymorphic relations between tables in drift. So you would have to manually add a "type" column to TagItem to describe whether the item references composers, people or events. That also means that you can't rely on the foreign key capabilities of sqlite3 for that reference.

I haven't heard of this feature before and I couldn't find too many other database libraries supporting it either. I'm not sure if drift should support this given that it's not supported by databases and requires rewriting queries at runtime.

simolus3 avatar Nov 28 '24 12:11 simolus3