The function for batch insertion only functions with Postgresql and SQL Server databases

I am a beginner in JavaScript and I am currently working on creating a new restaurant. I have come across a code snippet that inserts a relation into a join-table:

await newRestaurant.$relatedQuery('tags', trx).relate(tagIds);

Is it not possible to do this with MySQL? What is the alternative solution for this issue? When testing with Postman, I received the following message:

"message": "batch insert only works with Postgresql and SQL Server"

The tags are associated with a model class called RestaurantTag, which contains fields like id and name. The relationship is many-to-many mapped with the Restaurant model.

export const createRestaurant: ExpressHandlerFn = async (req, res, next) => {
  try {
    const {
      name,
      description,
      address,
      cityId,
      priceTier,
      phoneNumber,
      schedule,
      tagIds,
    } = req.body;
    const { thumbnailImage, galleryImages } = req.files as MulterFields;
    const userId = req.session!.id;

    const createRestaurantTransaction = await Restaurant.transaction(async (trx) => {
      let newRestaurant = await Restaurant.query(trx).insertGraphAndFetch({
        name,
        description,
        priceTier,
        address,
        cityId,
        phoneNumber,
        schedule,
      });

      await newRestaurant.$relatedQuery('tags', trx).relate(tagIds);

If there is any information missing or if you can provide assistance on this matter, please feel free to share your insights! Your help is greatly appreciated.

Answer №1

I finally cracked the code,

      const tasks: Promise<any>[] = [];

      for (const tag of <number[]>tagIds) {
        tasks.push(newRestaurant.$relatedQuery('tags', trx).relate(tag));
      }

      await Promise.all(tasks);

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

Error message: Unable to assign type (Combining React, Typescript, and Firebase)

Just started using TypeScript and in the process of migrating my React app to incorporate it. Encountering some type issues with Firebase auth service that I can't seem to find a solution for. Any suggestions? import React, { useEffect, useState } f ...

How to only disable checkboxes that are currently checked in Angular 8

click here to view an image**I would like to know how I can disable only the selected/checked items on a p-listbox in Angular 8. Is it possible to achieve this by adding a specific property or using CSS? Currently, when I try to add the [disabled] proper ...

Building a PathString Tree

I encountered a similar issue like the one discussed in this post (Get a tree like structure out of path string). I attempted to implement the suggested solution but I am facing difficulties getting it to work within an Angular context. The concept involv ...

Using the Distinct keyword in a query can result in a failure

I noticed that when I add the DISTINCT keyword to my operator column, the query no longer functions properly. However, when I remove DISTINCT, the query runs smoothly. $resource= $con->query("SELECT date,factory,DISTINCT(operator)as operator,FROM My ...

A simple method to generate a distinctive identifier

After inserting a new row into a table, how can I retrieve the unique ID of that specific row without executing an additional query to fetch the last inserted element? For example, if I execute the following query: $query = mysql_query("INSERT INTO `user ...

Retrieving the nth date from a table of dates categorized as "approved."

In my database, I have a table filled with dates for the next 20 years, which I use for quick reference in my application. This table helps me determine information such as whether a date is a holiday or if it contains a specific indicator. My goal is to ...

Error message encountered: Missing property status in TypeScript code

An error occurs in the refetchInterval when accessing data.status, with a message saying "property status does not exist" chatwrapper.tsx const ChatWrapper = ({ fileId }: ChatWrapperProps) => { const { data, isLoading } = trpc.getFileUploadStatus.use ...

Utilizing a Dependency Injection container effectively

I am venturing into the world of creating a Node.js backend for the first time after previously working with ASP.NET Core. I am interested in utilizing a DI Container and incorporating controllers into my project. In ASP.NET Core, a new instance of the c ...

Create a d.ts file for Vue components that are written using Typescript

As a newcomer to Vue, I am eager to dive into creating components and publishing packages to NPM. My plan is to develop Vue (typescript) + Vuetify reusable components that can be easily installed from NPM into any of my projects. While I have successfully ...

Tips on saving a cookie using universal-cookie

I followed a solution on Stack Overflow to set a cookie in my React application. However, the cookie expires with the session. Is there a way I can make this cookie persist beyond the session so it remains even when the browser is closed and reopened? ex ...

Complex MYSQL query pulling data from multiple tables

I'm looking to execute a MYSQL query that selects users from "table_in" with the same format as table_1. table_1 id Field_id user_id value 1 9 1 "hello" 2 10 1 "Multi" 3 9 2 "Something" 4 1 ...

Issue with displaying tab icons in Ionic 4

After updating the versions of Angular, Cordova, and Ionic, I started experiencing an issue with the tab icons displaying partially. Specifically, when my app loads with 4 tabs, only the first and third icons are visible. However, upon touching one of the ...

Angular does not display HTML content until the requested http data has been fully loaded

I am experiencing an issue where certain HTML content does not load until the component has received data from an API call through a service. Below is the relevant code snippet: import { ApiService } from './services/api.service'; @Component({ ...

Error: The variable "$this" has not been defined in the AJAX function

Recently, I've been delving into the world of javascript and ajax. I'm trying to create a dynamic select option list similar to this: However, when attempting to compile using Google Chrome Developer tools (F12), I encounter an error like this: ...

Issue encountered while attempting to launch Angular2 project

Having trouble running my Angular2 project, every time I try to use the ng serve command I get an error: Here is the link: I've installed angular 2 using angular-cli with the following steps: 1. sudo npm install -g angular-cli 2. sudo ng new mi-apl ...

What is the process for configuring a TimePicker object in antd to send a Date with UTC+3 applied?

I have Form.Item and a TimePicker defined inside it. I am using this form to send a POST request, but when I try to post 16:35, it gets sent as 13:35. The UTC offset is not being considered. However, the CreationTime works fine because it utilizes the Da ...

Tips for Modifying the currentUrl identifier in Angular 2

I am trying to change the ID property of my currentUrl object within my component. My goal is for the ID to update and then fetch the corresponding data based on that ID. However, I keep encountering this error message: "Cannot assign to read only propert ...

Unleashing the full potential of Azure DevOps custom Tasks in VS Code and TypeScript: A guide to configuring input variables

Scenario: I have developed a custom build task for Azure DevOps. This task requires an input parameter, param1 The task is created using VS Code (v1.30.1) and TypeScript (tsc --version state: v3.2.2) Issue During debugging of the task, I am unable to pr ...

Error in Protractor Typescript: The 'By' type does not share any properties with the 'Locator' type

https://i.stack.imgur.com/8j2PR.png All the different versions Error. Protractor version : 5.2.0 npm : 3.10.10 node :6.9.5 typescript :2.6.0 The 'By' type does not share any properties with the 'Locator' type What is the solution to ...

Connecting with Inner Joins: The Path of Followed and Following

I recently received some guidance in another discussion regarding an issue I was facing while retrieving data from two MySQL tables. It turns out, I needed to utilize inner joins. Here are the details of my tables: USERS --id (int) --username (varch ...