Typeorm runs insert query instead of update query on save of existing entity for ManyToOne relationships
See original GitHub issueIssue 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
- Save valid to database
- Save main to database
- 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:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.Ive fixed this issue and fix will be released in
0.2.0-alpha.19