1

I'm working on simple asset manager database using peewee. Each asset can belong to one of the types: image or video.

Let's say it looks like this:

# MAIN ASSET
class Asset(Model):
    name = CharField()
    datetime = DateTimeField()
    miniature = CharField()
    preview = CharField()
    type_ = ForeignKeyField()

    class Meta:
        database = DB

# TYPE SPECIFIC
class Image(Model):
    resolution_x = IntegerField()
    resolution_y = IntegerField()
    channels = CharField()
    layers = IntegerField()

    class Meta:
        database = DB

class Video(Model):
    resolution_x = IntegerField()
    resolution_y = IntegerField()
    framerate = FloatField()
    bitrate = FloatField()
    duration = FloatField()

    class Meta:
        database = DB

I would like to keep all the assets in one table, asset specific fields in other and connect them with "type_" field (ex. using foreign key or something). How can I achieve that? I suppose foreign key is a bad approach since it requires a predetermined model.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Websterek
  • 39
  • 5
  • Maybe I'm misunderstanding, but I think you're thinking about this backwards. I would just put FK fields on `Image` and `Video` that point to the relevant asset (e.g. `asset = ForeignKeyField(Asset)`) – booshong Mar 31 '21 at 01:39
  • This is my "plan b" with few caveats like global searching problem. Convient will be searching throughout Asset table and use FK to connect with specific data for the asset. – Websterek Mar 31 '21 at 07:20

1 Answers1

0

It looks like you are trying to create a Multiple Table Inheritance. Maybe this thread could be helpful

bronek89
  • 34
  • 2