# DB::Serializable with JOIN?

**URL:** <https://forum.crystal-lang.org/t/db-serializable-with-join/2002>\
**Category:** Help & Support\
**Created:** [April 25, 2020, 6:23pm UTC](https://forum.crystal-lang.org/t/db-serializable-with-join/2002 "2020-04-25T18:23:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [April 25, 2020, 6:23pm UTC](https://forum.crystal-lang.org/t/db-serializable-with-join/2002/1 "2020-04-25T18:23:52Z")

</div>

I’m trying to use `DB::Serializable` to deserialize rows from a Postgres DB into Crystal objects. It works great when a row maps to a single object, but I can’t seem to get it to map to multiple objects:

```crystal
db.query_all <<-SQL, app_id, as: {App, User}
  SELECT apps.*, users.* -- I specify the columns explicitly but it would just be noise here
  FROM apps
    JOIN app_access ON app_access.app_id = apps.id
    JOIN users ON app_access.user_id = users.id
  WHERE apps.id = $1
SQL

```

Is there a way to get `crystal-db` to chop up a result row into two or more different objects?

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [April 25, 2020, 6:30pm UTC](https://forum.crystal-lang.org/t/db-serializable-with-join/2002/2 "2020-04-25T18:30:32Z")

</div>

I do this with [`neo4j`](https://github.com/jgaskins/neo4j.cr) like this:

```crystal
txn.exec_cast <<-CYPHER, {app_id: app_id}, {App, User}
  MATCH (user:User)-[:GRANTED_ACCESS_TO]->(app:App)
  WHERE app.id = $app_id
  RETURN app, user
CYPHER

```

But with SQL, an ORM has to chop up the row into whole objects. It seems `DB::Serializable` has enough information to do this, I just don’t know how to invoke that.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [April 25, 2020, 7:57pm UTC](https://forum.crystal-lang.org/t/db-serializable-with-join/2002/3 "2020-04-25T19:57:47Z")

</div>

Db Serializable is very basic: just fetch from a single table. No joins, no nothing. I recommend you use a proper ORM. Maybe we should deprecate and remove serializable because it’s a bit useless.

---

<div class="post-metadata">

**Author:** ![drhuffman12](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drhuffman12/32/715_2.png) [@drhuffman12](https://forum.crystal-lang.org/u/drhuffman12)\
**Post date:** [April 26, 2020, 12:55am UTC](https://forum.crystal-lang.org/t/db-serializable-with-join/2002/4 "2020-04-26T00:55:09Z")

</div>

You might use `include JSON::Serializable`, `<class>.to_json` and `<class>.from_json`; and then try saving your deserialize objects.
