简体   繁体   中英

Peewee foreign key from one table to different tabels

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM