question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Two models referencing same table

See original GitHub issue

Version: 2.1.2 Problem: I have a post table that is responsible for a number of objects in our app (posts, blog posts, messages, replies, notifications, etc.) I would like to create a class for each and have that class instantiated with a scope (i.e. post_type == 'notification', post_type == 'message', etc.) each with their own custom relations.

In Laravel they call it global scopes, and have been going through the Objection.js documentation to see if there is something similar. The closest I found is Modifiers.

Preferred API:

// Posts
class Post extends Model {
  static get tableName() {
    return 'posts';
  }
}

// Message
class Message extends Model {
  static get tableName() {
    return 'posts'
  }

  function globalScope(query) {
    return query.where('post_type', '=', 'message')
  }
}

// Blog
class Blog extends Model {
  static get tableName() {
    return 'posts'
  }

  function globalScope(query) {
    return query.where('post_type', '=', 'blog')
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
wwei-fluxcommented, Jun 8, 2021

Curious to know if this is recognized as a pattern to handle the “multiple models sharing a table” aka. the Single Table Inheritance pattern. It’s a relatively common pattern that several concept “models” have similar underlying data structures but different business logic, so we want them to share a DB table, but separate models to separate logic.

I hope Objection.js can recognize it as a pattern/recipe and support it. The issue reported about upsertGraph in the last comment is a concern of whether we should use it or not.

1reaction
koskimascommented, Feb 11, 2020

$parseJson should work just fine. Another option is $beforeInsert and $beforeUpdate.

Oddly enough, I was able to remove the fromDatabaseJson method from the Person class you posted above. My inserts and queries are still working fine.

That was just for turning the items into correct class when they are queries through Post. Like this

const posts = await Post.query()

posts[0] instanceof Message
posts[1] instanceof Blog
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can two model use same table in laravel - Stack Overflow
You can create 2 models for the same table, but in your case you don't need to. Here is how you do the...
Read more >
How to Create a Model With Two Foreign Keys References ...
We would have User and Payment models/tables. They have a one-to-many relationship. A user has many payments and a payment belongs to two...
Read more >
Relations (Reference) - Prisma
A relation is a connection between two models in the Prisma schema. ... A one-to-many relationship between a user and posts table.
Read more >
How to create 2 foreign keys referencing the same table in ef ...
I want a user to have multiple conversations while each conversation has two users involved in the conversation, I tried this approach (as...
Read more >
Relationships between tables in a Data Model
A Data Model can have multiple relationships between two tables. To build accurate calculations, Excel needs a single path from one table to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found