Skip to content
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

When trying to update update: false column with @UpdateDateColumn the update column is updated #8393

Closed
rluvaton opened this issue Nov 20, 2021 · 4 comments · Fixed by #8394

Comments

@rluvaton
Copy link
Contributor

rluvaton commented Nov 20, 2021

Issue Description

Let's say I have this entity:

@Entity()
export class Post {

    @PrimaryGeneratedColumn()
    id: number;

    // Extra, not required column just for the example
    @Column()
    title: string;

    @UpdateDateColumn()
    lastUpdated: Date;

    @Column({
        // Forcing this column to only be written on insert time
        update: false
    })
    readOnlyColumn: number;
}

And I try to update the readOnlyColumn column

Expected Behavior

Should not update the readOnlyColumn AND should not update the lastUpdated column

Actual Behavior

Update the lastUpdated column

Steps to Reproduce

Reproduction Repo, It contains docker-compose.yml and the minimal code that is required to create this issue.

Just run npm i && npm start and it will start the DB and the "test"

My Environment

Plain TS

Dependency Version
Operating System Windows 11
Node.js version v16.6.2
Typescript version v4.5.2
TypeORM version v0.2.41

Additional Context

Relevant Database Driver(s)

I think this bug affects all DBs

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
@rluvaton
Copy link
Contributor Author

Added a failing tests represent this issue in the linked PR...

@spotykatch
Copy link
Contributor

Did that happen in earlier releases?

@rluvaton
Copy link
Contributor Author

Did that happen in earlier releases?

Yeah, just tested it in v0.2.20 (little after readonly option change to update) and it didn't work back then either...

@rluvaton
Copy link
Contributor Author

rluvaton commented Nov 22, 2021

I found the issue in the code...

if (metadata.updateDateColumn && updatedColumns.indexOf(metadata.updateDateColumn) === -1)
updateColumnAndValues.push(this.escape(metadata.updateDateColumn.databaseName) + " = CURRENT_TIMESTAMP"); // todo: fix issue with CURRENT_TIMESTAMP(6) being used, can "DEFAULT" be used?!

Few questions on how should we handle it:

  1. What should we do in case of trying to update update: false column? throw? (currently, it's not doing anything)

  2. Should we allow calling manager.update without columns when the update date/version column exists?

    e.g. Should we allow:

    await connection.manager.update(T, t.id, {});

    Currently, if they are not a UpdateValuesMissingError gonna be thrown...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants