New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UpdateDateColumn: multiple assignments to same column #2651
Comments
I faced a same problem... |
Same problem here |
I am also facing this issue currently. Job Entity with UpdateDate column: @Entity()
export class Job {
public static isValid(jobObj: JobObject): boolean {
return !!jobObj.startLocation && !!jobObj.endLocation;
}
...
@CreateDateColumn()
public dateCreated: Date;
@UpdateDateColumn()
public dateModified: Date;
@ManyToOne(type => Customer, (customer: Customer) => customer.jobs, {cascade: true})
public customer: Customer;
...
} I encountered this error when trying to write a unit test for updating the customer relation of the job entity: it("should change customer if different customer passed with job", async () => {
const newCustomer: CustomerObject = {companyName: "Brand New Company"};
const oldJobWithCustomer = await dbJob.getJobById(2, true);
oldJobWithCustomer.customer = newCustomer;
const newJob = await dbJob.updateJob(2, jobObj);
}); The above returns the error: and this is the failed query:
As with OP, the dateModified column is getting set twice, once with what I assume is the time the customer property was changed, and once with |
So far I've worked around (or perhaps this is what's expected) by parsing the JobObject and making sure it doesn't have it's own I encountered the same thing with foreign keys/relations. Where if I update the |
same issue, not happen with save method. |
its a bug. We need to exclude adding update column into the list if its already was set in the list provided by |
I guess you've meant exclude "updated" column, because bug not only in update column. As @akosasante, I'm getting multiple assignments to foreign key, while saving relation entities through cascades. |
Any progress here, as I have the same problem? |
It's still there, I must exclude that field before execute:
There seems to be some reason that this error has not been fixed. |
Do not manually assign |
I'm seeing a similar issue: "pg": "^7.11.0", The sql generated is : Code looks like so: let discover = await DiscoverResult.findOneOrFail(discoverId);
// later
await discover.update({
profileId: profile && profile.id,
userEmail: !user ? email : undefined,
result
}); Where async update(data: object) {
Object.assign(this, data);
await this.save();
return this;
} |
Looks like two @ManyToOne(_type => Profile, { eager: true, nullable: true })
@Field(_type => Profile)
profile: Profile;
@Column({ nullable: true })
profileId: string; Changing the update to this, solved my problem. await discover.update({
profile,
userEmail: !user ? email : undefined,
result
}); |
I have a simple save UPDATE "message"
SET "customerId" = $2,
"checkoutId" = $3,
"customerId" = $4,
"checkoutId" = $5,
"updatedAt" = CURRENT_TIMESTAMP
WHERE "id" IN ($1) RETURNING "updatedAt" As you can see Note: The parameters array is always correct - it has exactly 3 params ( The entity relationships are many-to-one and look like: @ManyToOne(_type => Checkout, checkout => checkout.messages, {
onDelete: 'SET NULL',
onUpdate: 'RESTRICT'
})
@JoinColumn({ name: 'checkoutId' })
@Index()
public checkout: Checkout | null;
@Column('text', { nullable: true })
public checkoutId: string | null; The error occurs quite rarely and I'm having a hard time reproducing but I do see it in my production app. |
I have code that always reproduce this issue: const p: Post = Post.findOne({id: 42});
await connection.getRepository(Post).update({id: 42}, p); |
To resolve this issue, please use
But I still got issue about foreign key, I want to define foreign key by myself. Maybe we will have
|
It seems like this error happens when you define some relationship and FK at the same time.
A definition like above caused I could avoid this error by adding
|
@pleerock awesome 👏 |
@pleerock |
Used repository.save() instead of repository.update() and it worked. |
The problem is in the eager loaded relations. Updating an FK manually while the relation property is loaded causes multiple assignments to the join column. The solution is either don't use the eager option, e.g: either set the relation property to undefined or delete it, e.g.: |
Agreed on that one. If anyone still believes the FK issue is a real bug and not user error please open a new issue with more info. For this issue it's off topic. Closing this issue as the original problem has been solved. |
Yes it's still a bug. At the least a better error message is required. Has anyone opened a new issue? |
The workaround of @evgenytk works for me. |
Yes, issue is actual. |
+1 |
Issue type:
[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[x]
postgres
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
[ ]
expo
TypeORM version:
[ ]
latest
[ ]
@next
[x]
0.2.7
Steps to reproduce or a small repository showing the problem:
I'm getting an error when updating with postgres using UpdateDateColumn.
Columns of entity:
Update function:
Logging:
Error:
We can see "updatedAt" = $18 and "updatedAt" = CURRENT_TIMESTAMP WHERE has appeared tiwce. I just want to suggest that we should ignore CreateDateColumn and UpdateDateColumn when generate query string?
Also I have tried to update successfully, but I cannot get the status update into UpdateResult:
Thanks so much.
The text was updated successfully, but these errors were encountered: