Two models referencing same table
See original GitHub issueVersion: 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:
- Created 4 years ago
- Comments:9
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.$parseJson
should work just fine. Another option is$beforeInsert
and$beforeUpdate
.That was just for turning the items into correct class when they are queries through
Post
. Like this