Utilizing raw queries in TypeORM with NestJS to enforce lowercase column names

Can anyone help me execute a query using nest/typeorm?

I'm utilizing Typeorm's "InjectConnection" to run a raw query in my Postgres Database. The issue arises with the column user_roles_role.userId (note that I am specifying 'userId' in uppercase). Here is the code snippet:

const queryText = `SELECT * FROM user_roles_role WHERE user_roles_role.userId = ${id}`

try {
  const rawData = await this.connection.query(queryText);
  return rawData;
} catch (err) {
  console.log(err);
  return err;
}

When running this query, I encounter an error where Typeorm seems to be converting the column name to lowercase, as shown in the Typeorm error message below:

query: 'SELECT * FROM user_roles_role WHERE user_roles_role.userId = 1', parameters: undefined, driverError: error: column user_roles_role.userid does not exist

Things I have attempted:

Using single quotes and double quotes (which didn't work)

Full Error Message:

"query": "SELECT * FROM user_roles_role WHERE user_roles_role.userId = 1", "driverError": { "length": 189, "name": "error", "severity": "ERROR", "code": "42703", "hint": "Perhaps you meant to reference the column >"user_roles_role.userId".", "position": "37", "file": "parse_relation.c", "line": "3599", "routine": "errorMissingColumn" }

Answer №1

When dealing with PostgreSQL's sensitivity to case, it is important to enclose column names in double quotes as shown below:

 `SELECT * FROM user_roles_role WHERE user_roles_role."userId" = ${id}`

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

What is the best way to adjust a string in SQL by substituting special characters?

During a recent interview, I was tasked with transforming a string by substituting special characters. The original string provided was 'AAA%BBB$CCC#DDD' The desired output is depicted in the image below: Could someone please explain how this ...

What is the best way to organize information in a table based on the date

This is my data table https://i.stack.imgur.com/1DNlj.png in the displayed table, the registration dates are sorted with the oldest date at the top. However, I aim to have the newest data displayed first. Below is the code snippet I am using: this.dataSo ...

Discover the utility of the useHistory() hook in TypeScript for Class Components

Hello there, I am currently attempting to implement the following code snippet in my TypeScript-based class component: this.history.push({ pathname: `/search-results`, search: `${job}$${location}` } ...

Can you provide guidance on how to specifically specify the type for the generics in this TypeScript function?

I've been diving into TypeScript and experimenting with mapped types to create a function that restricts users from extracting values off an object unless the keys exist. Take a look at the code below: const obj = { a: 1, b: 2, c: 3 } fun ...

Angular - Transform calendar dates to a lively green upon initial popup activation

I'm looking to customize my calendar so that all the dates in my list have a green background when the calendar is opened. ngOnInit(): void { this.roomService.getReservableDatesFromRoom(room.roomName).subscribe(data => { for (let i = 0; i ...

retrieve multiple entries using a single mysql query

Check out this table I created: id | question | The id's are organized as follows: 1000 -> 1050 2000 -> 2030 3000 -> 3099 4000 -> 4500 5000 -> 5010 I need to retrieve 5 records, one from each group of id. Is there a way to achieve ...

Ensuring strictNullChecks in Typescript is crucial when passing values between functions

When using the --strictNullChecks flag in TypeScript, there seems to be an issue with inferring that an optional property is not undefined when the check occurs in a separate function. (Please refer to the example provided, as articulating this clearly is ...

Creating a Summary Line in SQL Using the Over() Function

When using the over() function to calculate grand total, an issue arises when count() = 0. This results in those rows being added to the total even though they shouldn't be counted. The actual count is 138, not 141. How can I rectify this while still ...

Guide on transferring the token and user information from the backend to the front-end

Here is the code from my userservice.ts file export class UserService { BASE_URL = "http://localhost:8082"; constructor(private httpClient:HttpClient) {} public login(loginData:any){ return this.httpClient.post(this.BASE_URL+"/au ...

Tips for typing a narrow JSX.Element

Is there a way to create a variable in React that can be either a component or a string? Like this: function MyComponent(): JSX.Element { let icon: JSX.Element | string = "/example.png"; return <div>{typeof icon === "JSX.Element" ? icon : <i ...

Counting the occurrences of distinct fields in Oracle SQL

My task is to retrieve the id, type, and count of rows with unique combinations of id-type. Here is my attempt at solving it: SELECT ID, CASE WHEN /**/ THEN 'FIRST' WHEN /**/ THEN 'ANOTHER' ELSE &apos ...

Recursive CTE query used within an EXISTS statement

Currently, I'm facing a challenge with recursive CTEs on MariaDB/MySQL. The scenario is fairly straightforward: there's a users table, a roles table, and a user_roles table that links roles to users. However, roles can be hierarchical, and the ro ...

Emphasize a word in a Typescript text by wrapping it in an HTML tag

I've been experimenting with using HTML tags within TypeScript to highlight specific words in a sentence before displaying the sentence in HTML. Despite searching on StackOverflow for various solutions, I haven't been able to find one that works. ...

Navigating between pages has become challenging due to issues with the navbar, sidebar,

I successfully developed 4 Angular components: 1st component: menu 2nd component: header 3rd component: home 4th component: login The menu component features a sidebar/navbar created using Material UI. The login component consists of the login page. Howe ...

Step-by-step guide on building a wrapper child component for a React navigator

When using the Tab.Navigator component, it is important to note that only the Tab.Screen component can be a direct child component. Is there a way in Typescript to convert or cast the Tab.Screen Type to the TabButton function? const App = () => { retur ...

Ways to obtain the introductory data from every group

Below is a table that I am working with: id group name 1 2 dodo 2 1 sdf 3 2 sd 4 3 dfs 5 3 fda .... I am trying to retrieve the first record from each group as shown below: id group name ... 1 sdf 2 dodo 3 ...

There is no index signature that includes a parameter of type 'string' in the specified type

I have a background in mobile app development and am looking to learn more about TypeScript. How can I declare a map object with the form [string:any]? The error is occurring at line: map[key] = value; Element implicitly has an 'any' type becaus ...

creating a new database through django migration process

Currently using python 2.7 and django 1.8. Encountering the same issue as described in this link. A suggested solution found in the comments is: To resolve, I recreated the entire database, cleared migration history, and folders. I have reservations abo ...

Is implementing client components in Server Side pages an effective strategy for optimizing SSR performance?

In order to overcome the challenge of using client-side components in server-side pages, I made the decision to create a client-side wrapper to encapsulate these components within server-side pages. This way, I can manage all API calls and data fetching on ...

Should mutators be encapsulated within a class contained in a JS Module for better code organization and maintainability?

In order to maximize functionality of our new product using JavaScript, we have implemented an Authentication module that manages a tokenPromise which is updated upon user logins or token refreshes. It seems imperative to allow for mutation in this process ...