PostgreSQL reverse relationship

I have a table in my database called "textDate"

class TextData extends BaseEntity{
 
id(primaryGeneratedColumn)

ar:string

en:string

}

This entity is used to store all text in my project, such as titles, descriptions, and other fields that have foreign keys to it.

class object extends baseEntity{

id

@OneToOne()
@joinColumn()
title:TextData
...

I need to search through the text data and retrieve records based on certain conditions. I also want to get the objects that have foreign keys to these TextData records. Note: There are several types of objects that have access to the text data - object1 (with title), car (with title), career (with title). All of them have foreign keys to the TextData... Is there anything specific I can do? I've created:

select 'post' as Type,p3.id 
from post p3  
where p3."tagsId" =any ('{1,2,3}') or p3."titleId" =any ('{1,2,3}') 
 join text_data td 
 on td.id = p3."titleId" as  title
UNION  
select 'career' as Type,c2.id 
from career c2 
join text_data td 
 on td.id = c2."titleId" as  title
where c2."tagsId" =any ('{1,2,3}')

Answer №1

One suggestion is to store a reference to the child table in the text_data column, such as the name of the child table.

Your query contains errors that need to be corrected:

select 'post' as Type, p3.id 
from post p3  
join text_data td on td.id = p3."titleId"
where p3."tagsId" = any('{1,2,3}') or p3."titleId" = any('{1,2,3}')

UNION  

select 'career' as Type, c2.id 
from career c2 
join text_data td on td.id = c2."titleId" 
where c2."tagsId" = any('{1,2,3}')

An alternative approach for the query is as follows:

SELECT td.id, coalesce(p3.type, c2.type) AS type

FROM text_data td

INNER JOIN (SELECT "titleId", 'post' AS type FROM post WHERE "tagsId" = any('{1,2,3}') or "titleId" = any('{1,2,3}')) AS p3 ON td.id = p3."titleId"

INNER JOIN (SELECT "titleId", 'career' AS type FROM career WHERE "tagsId" = any('{1,2,3}')) AS c2 ON td.id = c2."titleId"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trigger the React useEffect only when the inputed string matches the previous one

Currently, I am in the process of creating my own custom useFetch hook. export const useFetch = <T extends unknown>( url: string, options?: RequestInit ) => { const [loading, setLoading] = useState(false); const [error, setError] = ...

Find the combined key names in an object where the values can be accessed by index

I am currently working on creating a function called indexByProp, which will only allow the selection of props to index by if they are strings, numbers, or symbols. This particular issue is related to https://github.com/microsoft/TypeScript/issues/33521. ...

How can I implement a dynamic progress bar using Ionic 4?

I am currently working on developing a progress bar that increases by 1% every time the user clicks a button. This is what my HTML code looks like: <ion-button *ngFor="let progress of progress" (click)="add(progress)">Progress</ion-button> &l ...

What are the steps to ensure a successful deeplink integration on iOS with Ionic?

Recently, I was working on a hybrid mobile app for Android/iOS using Nuxt 3, TypeScript, and Ionic. The main purpose of the app is to serve as an online store. One important feature involves redirecting users to the epay Halyk website during the payment pr ...

What is the best way to access the original observed node using MutationObserver when the subtree option is set to

Is there a way to access the original target node when using MutationObserver with options set to childList: true and subtree: true? According to the documentation on MDN, the target node changes to the mutated node during callbacks, but I want to always ...

What is the best way to trigger a method after an old component has been removed from the DOM while navigating within Angular

I am facing a challenge where I need to execute a method on ComponentB after a routerLink is clicked, causing the navigation from ComponentA to ComponentB. It is crucial that this method is triggered only after the entire navigation process is complete (i. ...

Error TS6200 and Error TS2403: There is a conflict between the definitions of the following identifiers in this file and another file

Currently working on setting up a TypeScript node project and running into issues with two files: node_modules@types\mongoose\index.d.ts node_modules\mongoose\index.d.ts Encountering conflicts in the following identifiers when trying ...

What is the best way to incorporate auto-completion into a browser-based editor using Monaco?

Recently, I embarked on a project to develop a browser-based editor using monaco and antlr for a unique programming language. Following an excellent guide, I found at , gave me a great start. While Monaco provides basic suggestions with ctrl + space, I am ...

Exploring Cypress: Leveraging the Power of Variable Assignment

Recently, I encountered an issue while working with a variable in a Cypress each loop. Despite incrementing the variable within the loop, it resets to zero once outside of it. Can someone shed light on why this happens and suggest a solution? Thank you. p ...

Is it possible to obtain Literal types for object keys dynamically in typescript?

I am looking to extract the type of object keys. Below is a generic function for objects with keys as strings: type GenericInput = { [key:string]: {value:string,type:HTMLInputTypeAttribute,placeholder:string,min?:number,max?:number,required?:boolean, err ...

Using TypeScript to specify a limited set of required fields

Can a custom type constraint be created to ensure that a type includes certain non-optional keys from an object, but not all keys? For instance: class Bar { key1: number key2: Object key3: <another type> } const Y = { key1: 'foo' ...

Is the ng-selector in Angular2 not sorting items alphabetically as expected?

This code snippet demonstrates the use of ng-selector in an .html file <ng-selector name="company" [(ngModel)]="company_selected" [formControl]="loanApplyForm.controls['company']" ...

Commitments, the Angular2 framework, and boundary

My Angular2 component is trying to obtain an ID from another service that returns a promise. To ensure that I receive the data before proceeding, I must await the Promise. Here's a snippet of what the component code looks like: export class AddTodoCo ...

Tips for identifying and handling errors in Playwright

I'm going crazy trying to handle a TimeoutError that I know is coming. Currently, I'm testing the Hidden Layers scenario from UI Testing Playground in Playwright Node.js and I want to see if there's a way to prevent the TimeoutError from cau ...

Enhancing Angular 4 classes with dependency injection techniques

Currently utilizing angular 4 and angular cli for my project development. I have created some classes that serve as the base for my components! However, as the constructors of these classes grow during development, I find myself in a phase where I need to ...

Leveraging Angular Observables for seamless data sharing across components

As I embark on developing my very first Angular app, I have encountered a challenge with filtering a list of book objects based on their gender attribute. The issue lies in sharing data between components – specifically the filteredData variable and the ...

The process of automatically formatting Typescript has transformed into an unfortunate auto-discarding action

Typescript autoformatting has become a concerning issue. Whenever I input quoted strings (" or `), the code surrounding it seems to temporarily glitch, with other strings appearing as code. This problem has recently escalated, particularly with strings li ...

Place the setState function within the useEffect hook

I'm currently working on a project that includes a "login" page. Once the user logs in, they should be directed to an interface displaying all their lists. To ensure this data loads immediately after login, I have implemented the useEffect hook and u ...

How can I emphasize the React Material UI TextField "Cell" within a React Material UI Table?

Currently, I am working on a project using React Material UI along with TypeScript. In one part of the application, there is a Material UI Table that includes a column of Material TextFields in each row. The goal is to highlight the entire table cell when ...

The 'admin' attribute is not found in the 'Object' data type

I have been facing this issue for quite some time now. The backend API response is indicating that a certain property does not exist, even though it clearly does. My Angular application suddenly started showing 18 errors today, and I am at a loss on how ...