# Single Table Inheritance

**URL:** <https://discourse.diesel.rs/t/single-table-inheritance/200>\
**Category:** Uncategorized\
**Created:** [November 21, 2020, 1:13pm UTC](https://discourse.diesel.rs/t/single-table-inheritance/200 "2020-11-21T13:13:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![polc](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@polc](https://discourse.diesel.rs/u/polc)\
**Post date:** [November 21, 2020, 1:13pm UTC](https://discourse.diesel.rs/t/single-table-inheritance/200/1 "2020-11-21T13:13:35Z")

</div>

Hi all ! I have the following schema and structs. I would like to get a `Vec<Question>` from a query. It doesn’t seem possible with Diesel but I’m maybe missing something? Also, is there a better architecture for this kind of schema, which respect the Diesel-way to do things ?

```sql
create type question_type as enum ('text', 'image');

create table "questions" (
    id uuid primary key not null,
    type question_type not null,
    label text,
    image_url text    
)

```

```auto
enum Question {
    TextQuestion(TextQuestion),
    ImageQuestion(ImageQuestion),
}

struct QuestionBase {
    id: String,

    // other fields ...
}

struct TextQuestion {
    base: QuestionBase,
    label: String,

    // other fields ...
}

struct ImageQuestion {
    base: QuestionBase,
    image_url: String,

    // other fields ...
}

```

---

<div class="post-metadata">

**Author:** ![weiznich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/weiznich/32/64_2.png) [@weiznich](https://discourse.diesel.rs/u/weiznich)\
**Post date:** [November 23, 2020, 9:10am UTC](https://discourse.diesel.rs/t/single-table-inheritance/200/2 "2020-11-23T09:10:30Z")

</div>

It is certainly possible to implement such a behavior with diesel, but I wouldn’t consider this as idiomatic database code.

As for the implementation:

- You need to implement a custom sql type mapping for your enum (`question_type`). See [here](https://github.com/diesel-rs/diesel/blob/3984dd9de9b91b335fa62a5c33cccd999cc3abfa/guide_drafts/custom_types.md) for an example.
- You need to provide a custom `Queryable` impl for your `Question` enum implementing your desired mapping there.

---

<div class="post-metadata">

**Author:** ![polc](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@polc](https://discourse.diesel.rs/u/polc)\
**Post date:** [November 24, 2020, 10:49am UTC](https://discourse.diesel.rs/t/single-table-inheritance/200/3 "2020-11-24T10:49:03Z")

</div>

Perfect, thanks @weiznich!
