The dynamic duo of MongoDB and Prisma: forging a groundbreaking one-to-many relationship

Is it possible to establish a one-way m-to-n relationship without requiring both collections to have each other's ids? I am attempting the following:

model Country {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  name            String    @unique
  users           User[]
}

model User {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  userName        String    @unique
  countryIds      String[]  @db.ObjectId
  countries       Country[] @relation(fields: [countryIds], references: [id])
  // ....
}

However, Prisma is forcing me to add another field to Country to store the user ids... Like this:

model Country {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  name            String    @unique
  userIds         String[]  @db.ObjectId
  users           Player[]  @relation(fields: [userIds], references: [id])
}

I do not require that data and it does not make sense logically. Is there a way to avoid this? Any potential workarounds?

Answer №1

Through my experimentation, I discovered that even if the userIds field is absent in any document, the user's countries can still be accurately queried. The only minor drawback (if you can call it that) is the inability to query users based on their countries. However, this limitation seems reasonable because if there was a need to perform such queries, establishing a relationship between userIds and countries would be logical. The actual issue lies in the error that prevents me from compiling/running the code without including userIds, despite it functioning properly without it.

UPDATE:

This is how my schema turned out after some adjustments @Min Somai:

model Country {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  name            String    @unique
  userIds         String[]  @db.ObjectId
  users           User[]    @relation(fields: [countryIds], references: [id])
}

model User {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  userName        String    @unique
  countryIds      String[]  @db.ObjectId
  countries       Country[] @relation(fields: [countryIds], references: [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

Improving an HTML list with JavaScript

My current project involves a JavaScript game similar to Scrabble. I want users to be able to create new words in the game, and have those words added to a list that is displayed on the page within a div element. However, I'm struggling to understand ...

Error code TS1005: Compiling Typescript DefinitelyTyped assets encountered an issue

I'm confident in my setup, but I can't seem to get tsc to compile. Here's the branch of my repository: https://github.com/inosion/sample-atom-typescript-package/tree/add_react Although I have the latest versions of typescript, I'm uns ...

Looking to upload a .json file into MongoDB using Java programming

I'm new to MongoDB and I have it up and running. My task is to insert the Student.json file into MongoDB using Java code instead of mongoimport. Student.java public class Student { @Id private ObjectId Id; private long studentId; private String st ...

A guide to filtering a JSON array by email address with JavaScript

How can I identify distinct elements in a JSON array using JavaScript? Below is the example of my JSON array. I need to determine the size of unique elements. [ { "_id": "5aaa4f8cd0ccf521304dc6bd", "email": "<a h ...

What could be causing the abundance of API calls from Yandex Metrica?

In my Nextjs SPA, there is an iframe widget that is rendered using React. Inside this widget's index.html file, I have inserted the Yandex Metrica script and set up a goal tag to track user clicks on the registration button. The goal tracking is wor ...

"Uh-oh, looks like my computer is having trouble locating those scripts - I

I am in the process of creating a small website and have listed my script references below. <!doctype html> <html class="no-js" lang="en" ng-app="App"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device- ...

Can I send an array of JavaScript classes to an MVC controller?

I'm struggling to pass an array of services to my controller. I've experimented with different methods, like serializing the data before sending it to the controller, serializing each service individually, and converting the controller parameter ...

Attempting to serialize a form using Ajax and jQuery

I am facing an issue with submitting user input from a modal using jQuery and AJAX. The problem is that the AJAX call is not capturing the user input. Even though the AJAX call is successful, when I check on the server side, the user input appears to be bl ...

The error message "TypeError: res.json is not a function in express.router" indicates that

I encountered the following error message and I'm not sure how to resolve it: TypeError: res.json is not a function I have reviewed the express documentation but couldn't find any syntax errors or issues. Here is my code: index.js import expr ...

Modify background image upon hovering using AngularJS

I cannot seem to make the background images of my divs change. Despite trying various options, none of them have been successful. Here's an example of my code: <div ng-controller="mainController" class="main"> <div ng-repeat="land in lan ...

Is it better to utilize AngularJS $http service for making requests, or should I opt for jQuery AJAX if available

When working on my project, I rely on the angularjs framework and always enjoy utilizing the $http service for ajax calls. However, I have come across situations in the project where the UI is not directly impacted by the ajax call and angularjs bindings a ...

When the input field is clicked, the file:/// URL is sent

Currently, my HTML page contains a form that includes an input field for URLs. Ideally, upon typing in the URL and clicking the button, I intend to be redirected to that website. However, the issue lies in the fact that instead of redirecting me to the p ...

What is the counterpart of $.isEmptyObject({}) in Typescript for checking if an object is

Is there a formal method for testing an Object (response from server) to see if it is empty? Currently, I am using jQuery to accomplish this. this.http.post(url, data, {headers: headers}).then( result => { if (!$.isEmptyObject(result ...

Design incisions on Cylinder Structure

I am facing a challenge while attempting to replicate a scene. The specific detail I am struggling with is quite intricate. The scene features a surface made of cylinder geometry with numerous small circular notches. Inside these notches, there are white ...

Implementing dynamic class bindings with Vue.js using v-for

I am currently using a v-for loop in Vue.js to create a list of items populated with data from an API. Here is an example of the items array: items: [ { foo: 'something', number: 60 }, { foo: 'anything', ...

It is impossible to alter the data contained within the input box

Objective: Upon clicking a button to display the modal, the selected data (first and last name) should be inserted into an input box and editable. The user should be able to modify the data. Issue: The data entered into the input box cannot be edited ...

Ensure the text value of a collection of web elements by utilizing nightwatch.js

Recently, I started using nightwatch.js and I am trying to retrieve a list of elements to verify the text value of each element against a specific string. Here's what I have attempted: function iterateElements(elems) { elems.value.forEach(funct ...

Employing eval for parsing the JSON data

function ajaxFunction(){ var ajaxRequest; // The variable that enables the use of Ajax technology! try{ // Compatible with Opera 8.0+, Firefox, and Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // For Internet Explorer Browsers ...

Encountering a series of errors in Discord.js v14 while executing a specific

UPDATE :- After troubleshooting, I discovered that the previous error stemmed from my command handler itself! However, now I am encountering a whole bunch of new errors... actually, multiple errors. So, I'm in desperate need of assistance. (Note: Apol ...

Im testing the creation of a global style using styled-components

Is there a way to create snapshot tests for styled-components with createGlobalStyle? The testing environment includes jest v22.4.4, styled-components v4.1.2, react v16.7, jest-styled-components v5.0.1, and react-test-renderer v16.6.3 Currently, the outp ...