question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typeorm runs insert query instead of update query on save of existing entity for ManyToOne relationships

See original GitHub issue

Issue type:

[x] bug report

Database system/driver:

[x] postgres

TypeORM version:

[x] @next

Relevant models

@Entity("main") 
export class mainModel extends BaseEntity {
	@Column({ type: "bigint", unique: true, primary: true })
	@Generated("increment")
	id: string;
	
	//other columns ommited
	@OneToMany(
		type => dataModel,
		dataModel=> dataModel.id,
		{ cascade: true, eager: true }
	)
	data: dataModel[];	
}

@Entity("valid")
export class validModel extends BaseEntity {
	@Column({
		type: "text",
		primary: true
	})
	valid: string;

	@Column({
		type: "boolean",
		default: "true"
	})
	active: boolean;

	@OneToMany(
		type => SourceOptionsDataModel,
		sourceOptionsDataModel => sourceOptionsDataModel.sourceOption
	)
	sourceOptionsData: SourceOptionsDataModel[];
}

@Entity("data")
export class dataModel extends BaseEntity {
	@ManyToOne(type => validModel , {
		primary: true,
		eager: true
	})
	@JoinColumn({
		name: "valid",
		referencedColumnName: "valid"
	})
	valid: validModel;

	@ManyToOne(type => RmaModel, {
		primary: true
	})
	@JoinColumn({
		name: "mainId",
		referencedColumnName: "id"
	})
	id: mainModel ;

	@Column({
		type: "boolean",
		default: false
	})
	checked: boolean;
}

Example Data

valid

{
"valid": "somevalue"
}

main

{
"data": [{"valid": "somevalue", "checked": true }]
}

Steps to reproduce

  1. Save valid to database
  2. Save main to database
  3. Attempt to update main yields issues

Example code to produce issue

const theId = \\the id you get from saving main
const theManager = getManager();
const theModel: any = await theManager.findOne(model, theId);
const theSave = await theManager.save(model, theModel);

Issue Typeorm attempts to to run a insert query instead of a update query for values in dataModel entity.

Attempts to resolve Added id to data array in main.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
MardariGcommented, Aug 9, 2018

I’m with 0.2.7 version, and still have this issue smh…

UPDATE: My bad. The ID that I provided to entity on update was of type string, it was parsed from req.params.id as '3', however it is of type number in entity. Parsing the to number fixed the problem.

0reactions
pleerockcommented, Feb 19, 2018

Ive fixed this issue and fix will be released in 0.2.0-alpha.19

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to save relation in @ManyToMany in typeORM
Here's my question: How to save the relation? My code as below: @Entity() export class Article { @PrimaryGeneratedColumn() id ...
Read more >
Developers - Typeorm runs insert query instead of update ...
Typeorm runs insert query instead of update query on save of existing entity for ManyToOne relationships.
Read more >
Many-to-one / one-to-many relations - typeorm - GitBook
Let's take for example User and Photo entities. User can have multiple photos, but each photo is owned by only one single user....
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be...
Read more >
TypeORM By Example: Part 6 - codeburst
Let us say our todos have a single author and authors can have multiple todos; this is a one-to-many relationship between authors and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found