typeorm: Array type default value doesnt work. PostgreSQL

Issue type:

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

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] websql

TypeORM version:

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

Steps to reproduce or a small repository showing the problem: My entity column:

@Column("float4", {
	array: true,
	default: [ 0.0, 0.0, 0.0 ],
	nullable: false
})
public testFloatArray: number[];

This generate query:

ALTER TABLE "public"."test" ADD "testFloatArray" real array NOT NULL DEFAULT '[0,0,0]'

And i got an error: error: malformed array literal: "[0,0,0] The PostgresSQL syntax should be:

ALTER TABLE "public"."test" ADD "testFloatArray" real array NOT NULL DEFAULT '{0,0,0}'

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (6 by maintainers)

Commits related to this issue

Most upvoted comments

While it’s nice to have a workaround, it still feels like default: [] should work and this should be open.

@pleerock please reopen this. I agree that default: [] should work, it’s super intuitive.

Hi @thematan. Typeorm from version 0.2.12 support define default enum array as array of typescript enum https://github.com/typeorm/typeorm/pull/3414:

@Column({
    type: "enum",
    enum: StringEnum,
    array: true,
    default: [StringEnum.ADMIN]
})
stringEnums: StringEnum[];
default: "{0.0, 0.0, 0.0}",

Works fine

If someone comes here to search for initializing empty array this works:

default: ()=>‘array[]::integer[]’

any update on this ? We have a thing with our migrations regarding this. Our column:

@Column({ type: 'text', nullable: false, array: true, default: 'array[]' })
someColumnName: string[]

and although the column is creted on the DB just fine, the migrations are keep being generated for this column like so:

await queryRunner.query(`COMMENT ON COLUMN "event"."someColumnName" IS NULL`);
await queryRunner.query(`ALTER TABLE "event" ALTER COLUMN "someColumnName" SET DEFAULT 'array[]'::text[]`);

we have to manually delete them from the migration file each time, it’s frustrating.

We tried also default: '{}', default: 'ARRAY[]' nothing works. the column migration keep being generated each time

You can do:

@Column("float4", {
	array: true,
	default: () => [ 0.0, 0.0, 0.0 ],
	nullable: false
})
public testFloatArray: number[];

Just in case it helps anyone… This seems to work for me, setting the default as an empty string when using simple-array type:

@Column({ type: 'simple-array', default: '' })
pushTokens: string[];

@joekendal Try this:

default: () => 'array[]::text[]'

@pleerock your decision gives an error. Error:

error: syntax error at or near "0"

Query:

ALTER TABLE "public"."test" ADD "testFloatArray" real array NOT NULL DEFAULT 0,0,0