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

MariaDb VIRTUAL columns don't accept [NULL | NOT NULL] #2691

Closed
StrikeForceZero opened this issue Aug 21, 2018 · 1 comment · Fixed by #7022, mattwelke/typeorm-postgres-example#165 or newerton/gobarber-2-backend#17

Comments

@StrikeForceZero
Copy link
Contributor

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

import {
    Column,
    Entity,
    PrimaryColumn,
} from 'typeorm';

@Entity()
export class Foo {
    @PrimaryColumn()
    public id: number;

    @Column({
        name: 'ipv4_bin',
        type: 'varbinary',
        length: 16,
    })
    public ipv4_bin: string;

    @Column({
        name: 'ipv4_text',
        type: 'varbinary',
        length: 16,
        asExpression: 'INET_NTOA(ipv4_bin)',
        generatedType: 'VIRTUAL',
    })
    public readonly ipv4_text: string;
}

This more or less creates this schema:

CREATE TABLE `foo` (
  `id`        int                                            NOT NULL,
  `ipv4_bin`  varbinary(16)                                  NOT NULL,
  `ipv4_text` varbinary(16) AS (INET_NTOA(ipv4_bin)) VIRTUAL NOT NULL,
  PRIMARY KEY (`id`)
)

The issue is https://github.com/typeorm/typeorm/blob/master/src/driver/mysql/MysqlQueryRunner.ts#L1500-L1503
adds the [NULL | NOT NULL] on a virtual column, which is fine in MySQL 5.7, but apparently not in mariaDB... I can't find anything hinting this in their documentation. Only Errors and articles saying so.

https://mariadb.com/kb/en/library/generated-columns/

https://www.percona.com/blog/2016/03/04/virtual-columns-in-mysql-and-mariadb/

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 'NOT NULL, PRIMARY KEY (`id`)'

Adding a check to the lines I referred to above

if (!(isMariaDb && column.asExpression) && !column.isNullable)
            c += " NOT NULL";
if (!(isMariaDb && column.asExpression) && column.isNullable)
            c += " NULL";

seems to work fine.

While the handling of VIRTUAL [NULL | NOT NULL] should probably need to be confirmed by mariaDb and documented, its current iteration doesn't allow it.

@ceteras
Copy link

ceteras commented Mar 4, 2019

This issue affects creating geometry virtual columns. A spatial index cannot be created for them as it requires a geometry NOT NULL definition.

pleerock added a commit that referenced this issue Feb 8, 2021
* Fix MariaDB VIRTUAL [NOT NULL|NULL] error

Fixes #2691

query failed: ALTER TABLE `customer` CHANGE `fullName` `fullName` varchar(255) AS (CONCAT(`firstName`, ' ', `lastName`)) VIRTUAL NULL
error: Error: 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 'NULL' at line 1

* slightly change code style to improve readability

Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment