Skip to content

MariaDB: When collation is set for Generated Column, the query generated by migration:generate is a syntax error. #8761

@mdoi2

Description

@mdoi2

Issue Description

When collation is set for Generated Column, the query generated by migration:generate is a syntax error.

My Environment

Dependency Version
MariaDB version 10.6.5
TypeORM version 0.2.45

Relevant Database Driver(s)

DB Type Reproducible
mysql yes

Steps to Reproduce

  1. Create this entity class.
import { Column, PrimaryColumn, Entity } from 'typeorm';

@Entity()
export default class SampleEntity {
  @PrimaryColumn({
    type: 'int',
  })
  id!: number;

  @Column({
    type: 'varchar',
    length: 100,
    collation: 'latin1_bin',
    asExpression: 'CONCAT("a", id)',
    generatedType: 'VIRTUAL',
  })
  target!: string;
}
  1. Execute migration:generate.

Here is the query generated as a result of running the automatic generation.

CREATE TABLE `sample_entity` (`id` int NOT NULL, `target` varchar(100) AS (CONCAT("a", id)) VIRTUAL COLLATE "latin1_bin", PRIMARY KEY (`id`)) ENGINE=InnoDB;

When this query is executed on MariaDB, the error shown here is output.

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COLLATE "latin1_bin", PRIMARY KEY (id)) ENGINE=InnoDB' at line 1

Additional Context

The cause of this problem appears to be the location of the COLLATE specification.
Moving the COLLATE specification to after the column type specification works well.

Therefore, I think the order of the code in the buildCreateColumnSql method of the MysqlQueryRunner class needs to be rearranged to solve this problem.

Perhaps you could move the append from column.asExpression to around just after the COLLATE specification.

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