Skip to content

asExpression column altered on every sync #4669

@zortoflaven

Description

@zortoflaven

Issue type:

[ ] question
[x ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Local development against a default install of MySQL8 Community Edition.

@Entity("metadataversion")
export class metadataversion {

    @Column({
        primary: true,
        nullable: false,
        type: "varchar",
        length: 255,
        name: "MetaDataVersionOID",
        asExpression: "concat('MDV.',upper(replace(`Name`,' ','')))",
        generatedType: "STORED"
    })
    MetaDataVersionOID: string;

    @Column("varchar", {
        nullable: false,
        length: 45,
        name: "Name"
    })
    Name: string;
}

I have synchronize: true in my config.

When starting the server, typeorm will alter the MetaDataVersionOID column every time:

[0] query: ALTER TABLE metadataversion CHANGE MetaDataVersionOID MetaDataVersionOID varchar(255) AS (concat('MDV.',upper(replace(Name,' ','')))) STORED NOT NULL

To try and see why typeorm was thinking this column was different, I went into driver/mysql/MysqlDriver.js and uncommented out line 641:

console.log("asExpression:", tableColumn.asExpression, columnMetadata.asExpression);

When I run now, typeorm logs the following:

[0] asExpression: concat(_utf8mb3\'MDV.\',upper(replace(`Name`,_utf8mb3\' \',_utf8mb3\'\'))) concat('MDV.',upper(replace(`Name`,' ','')))

I tried modifying my expression to include the additional info to make them match like so:

    @Column({
        primary: true,
        nullable: false,
        type: "varchar",
        length: 255,
        name: "MetaDataVersionOID",
        asExpression: "concat(_utf8mb3\\'MDV.\\',upper(replace(`Name`,_utf8mb3\\' \\',_utf8mb3\\'\\')))",
        generatedType: "STORED"
    })
    MetaDataVersionOID: string;

This works when the table already exists; I no longer see any ALTER statement. However, when creating the table when it doesn't exist, it looks like typeorm adds more escapes and I end up with an expression like:

"concat(_utf8mb3\\\'MDV.\\\',upper(replace(`Name`,_utf8mb3\\\' \\\',_utf8mb3\\\'\\\')))"

Which MySQL fails on.

Not a huge issue as the alters don't result in any data loss and we will have synchronize: false set for production, but any ideas here? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions