TypeORM's OneToMany relationship creates a corresponding column in the database

I am working with three tables and entities:

  • client
  • store
  • store_client

The relationships between these entities are as follows:

Client:

@OneToMany((type) => StoreClient, (storeClient) => storeClient.client, { eager: false })
storeClient: StoreClient[];

Store:

@OneToMany((type) => StoreClient, (storeClient) => storeClient.store, { eager: false })
storeClient: StoreClient[];

StoreClient:

@ManyToOne((type) => Client, (client) => client.storeClient, { eager: false })
client: Client;

@ManyToOne((type) => Store, (store) => store.storeClient, { eager: false })
store: Store;

However, I have encountered an issue when creating migrations and running them. The structure of the store_client table is correctly generated with columns for id, clientId, storeId, and Role.

The problem lies in the store and client tables where additional columns like storeClientId are created. Can anyone provide guidance on how to resolve this?

Thank you in advance for any assistance provided!

Answer №1

Resolved the problem caused by caching by disabling cache in ORM configuration file and recreating migrations to clear it.

Answer №2

The reason for this behavior is due to the presence of @OneToMany relationships set up on Shop and Customer in TypeOrm, which results in the creation of a foreign-key column linking them as ShopCustomer.

In its many-to-one documentation, TypeOrm explicitly states that "If you are only interested in the @ManyToOne relationship, you can define it without requiring @OneToMany on the associated entity."

To resolve this issue, simply remove the @OneToMany relationships from Shop and Customer entities.

Answer №3

The relationships seem to be in order. It's possible that the previous relationships were incorrect. Try deleting the dist folder in the root directory to clear the entities cache. As mentioned earlier, the @OneToMany annotations may not be required and are not expected to create any additional fields.

Another possible solution is to use the following code snippet:

@OneToMany((type) => ShopCustomer)
shopCustomer: ShopCustomer[];

The inverse relationship in @ManyToOne is not needed, and by default, eager loading is set to false.

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

The fixed-top navigation bar in Bootstrap obscures the links within the #content

As a student studying software development, I am currently working on a web development project for my class. Utilizing Bootstrap, I have implemented a navbar-fixed-top and structured the body as a table-striped table with each row containing a <a href= ...

``KnockoutJS offers a variety of options for handling options, including optionsText

I am curious about the functionality of data-bind, options, optionsText, and optionsValue in this code snippet: <div class="header dropdown"> <select data-bind=" value: locale.selected_locale, options: [{ value: 'en-CA&ap ...

Changing the location.hash parameter prior to the page loading

Imagine a scenario where a page contains only the following script: <script type="text/javascript"> window.location.hash = 'testing'; window.history.back(); </script> When you navigate to google.com and then load this page, you& ...

Insert a value within each iteration of ng-repeat

Is it possible to iterate over a set of items and increment the value of X by 10 each time it is repeated? For example: <rect ng-repeat="a in b" x="0" y="0" fill="#00000" width="206.2" height="117.7" class=" {{ a.name }} " /> I am looking to achie ...

Implement a logging system to track and record data from both incoming requests and outgoing responses on a server powered by Express and Node.js

Is there a way for my server to log the response and request data when posting to another server? Thank you. const request = require('request'); postToIotPlatform = function postToIotPlatform(req, res, next) { var formData = JSON.stringify( ...

javascript - Transferring content from one div to another document

Managing a small website that includes both mobile and desktop index pages can be quite a task. To streamline the editing process, I am looking for a way to modify one index page while automatically generating the content for the other from it. This will h ...

I am experiencing an issue with my date filter where it does not display any results when I choose the same date for the start and end dates. Can anyone help me troub

Having an issue with my custom filter pipe in Angular. When I select the same dates in the start and end date, it doesn't display the result even though the record exists for that date. I've noticed that I have to enter a date 1 day before or ea ...

Is it possible to pass an array to a class constructor in JavaScript using destructuring?

I am interested in developing a Statistics class that can handle 25 inputs (or possibly more or less) and perform calculations to find values such as mean, median, mode, range, variance, and standard deviation. Is it possible to achieve something like thi ...

Communication between the server and client using MQTT

My goal is to establish communication between server and client using mosca. In the initial stage, I am able to subscribe and publish data from the client to the server successfully. However, I encountered a problem when trying to publish data from the ser ...

Angular is throwing an error stating that the type '{ }[]' cannot be assigned to the type '[{ }]'

I'm in need of assistance and clarification regarding the error I encountered in my application... When I receive a JSON response from an API with some data that includes an array of products, I aim to extract these products (izdelki) from the array, ...

Error encountered in NEXT JS: Unable to parse URL from /api/projects or Error message: Failed to connect to 127.0.0.1:3000

Currently utilizing: export const getStaticProps = async () => { export const getStaticPaths = async () => { and accessing my API (pages/api/projects/) created with Next.js on my local host const res = await fetch("http://localhost:3000/api/ ...

include choices to .vue document

When looking at Vue documentation, you may come across code like this: var vm = new Vue({ el: '#example', data: { message: 'Hello' }, template: `<div> {{ message }} </div>`, methods: { reverseM ...

Alert: Ajax encountered an issue with the auto-refreshing field

When running a script I created for a self-updating field, I encountered the following error message: UpdateField.html:37 Uncaught ReferenceError: fieldname is not defined at HTMLInputElement.onchange (UpdateField.html:37) Script: https://i.sstatic.n ...

Unable to locate element within the HTML code in Angular

Despite using document.getElementByID, I am unable to retrieve the value of the list element in the HTML. The code shows that the element is undefined, even though it has the same id. Here is the snippet of code: fileupload.component.ts: ngOnInit() { ...

Generating Bootstrap Vue Dropdown components in real time

I am currently utilizing Bootstrap Vue to construct a dynamic Dropdown component that can render different elements such as item, title, and divider. Is there a method to accomplish this task effectively? The desired outcome for the Dropdown component wou ...

Having trouble rendering JSON data on a FlatList component in React Native

After expanding FlatList in console.log and verifying the JSON value, I am facing an issue where the values are not displaying on the list. The data is being passed to the second screen and displayed there, but the first screen remains blank. Any assistanc ...

Using VueJS to apply a single component to multiple routes in a neat package

I have created three routes in my web app: /login, /signup, and /forgot, each with their own components containing basic forms. I want to include these components within a landing page component without integrating the landing page logic directly into the ...

What steps should I take to stop material-ui Select options from opening when clicking on specific parts of the selected option?

Presently, I am utilizing a Select component from @material-ui/core/Select, which contains only one option for simplification purposes, and the code snippet is as follows: <FormControl> <InputLabel id="demo-controlled-open-select-label">Test ...

Combine each module using grunt and then compress

Looking to achieve this using Grunt. My current file structure is as follows: /app --app.js --/module1 ----module1.js ----module1Model.js --/module2 ----module2.js ----module2Model.js How can I bundle and minify each module into a single file to appear ...

Choosing the state object name dynamically within a React JS component

I have a quick question about updating state in React. How can I change a specific object in a copy of the state that is selected using e.target.name and then set to e.target.value? For example, if I want to change newState.age when e.target.name = age i ...