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

Array type default value doesnt work. PostgreSQL #1532

Closed
CocaColaBear opened this issue Jan 31, 2018 · 20 comments · Fixed by #7409
Closed

Array type default value doesnt work. PostgreSQL #1532

CocaColaBear opened this issue Jan 31, 2018 · 20 comments · Fixed by #7409
Labels

Comments

@CocaColaBear
Copy link

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}'
@pleerock
Copy link
Member

You can do:

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

@CocaColaBear
Copy link
Author

@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

@CocaColaBear
Copy link
Author

default: "{0.0, 0.0, 0.0}",

Works fine

@pleerock
Copy link
Member

👍

@thematan
Copy link

thematan commented May 3, 2018

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

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

@thematan
Copy link

thematan commented Feb 10, 2019 via email

@vlapo
Copy link
Contributor

vlapo commented Feb 10, 2019

Hi @thematan. Typeorm from version 0.2.12 support define default enum array as array of typescript enum #3414:

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

bobbywang000 added a commit to bobbywang000/nemosyne that referenced this issue Jun 5, 2020
See typeorm/typeorm#1532, since simple-array is stored as text under the hood, we need to set the default for `events` to an empty string, rather than an empty array.
bobbywang000 added a commit to bobbywang000/nemosyne that referenced this issue Jun 5, 2020
* Add Uniqueness Constraints for DateRange, Entry, Tag

DateRange: start/end dates have to be unique
Entry: written date and subject date have to be unique
Tag: name has to be unique

* Make DateRange.title optional

* Set default for DateRange.events to empty string

See typeorm/typeorm#1532, since simple-array is stored as text under the hood, we need to set the default for `events` to an empty string, rather than an empty array.

* Add cascades

Set most cascades to ['insert', 'update'] to reduce risk of data loss if a single deletion causes a TON of data to be deleted. Exception: deleting a `Tag` deletes all the associated `Note`s.
@joekendal
Copy link

@thematan what about for an array of type string? 'array[]::string[]' doesn't work 🤨

@jtrein
Copy link

jtrein commented Oct 14, 2020

@joekendal Try this:

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

@LyricL-Gitster
Copy link

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

@fr1sk
Copy link

fr1sk commented Dec 25, 2020

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

@gabimor
Copy link

gabimor commented Jan 27, 2021

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

@nebkat
Copy link
Contributor

nebkat commented Jan 27, 2021

I will try to incorporate this into #7282, but could someone give me a few examples of the expected inputs/outputs? What happens if the array contains mixed types, nulls, etc?

@gabimor default values defined as strings are treated as string values. You must use the () => 'array[]' syntax if you want it to be treated as a raw value.

@edeesis
Copy link
Contributor

edeesis commented Feb 22, 2021

I have submitted a PR with a couple of test cases that I would expect would work. If anyone has any others they'd like to add, I can add them to my PR.

kauz added a commit to kauz/typeorm that referenced this issue May 25, 2021
AlexMesser pushed a commit that referenced this issue May 30, 2021
…7681)

* fix: correctly strip type conversion in postgres for default values

Use better regex to remove only type conversion, not the whole string to the end

Closes: 7110

* fix: better regex

* fix: modify postgres default regex to not break #1532

* fix: modify postgres default regex to support user-defined types

This makes sure updated regex does not break migrations chnages introduced in #7647

* test: add test case for #5132
@c1moore
Copy link

c1moore commented Jul 1, 2021

Has this reverted? I'm seeing this issue in v0.2.34

@edeesis
Copy link
Contributor

edeesis commented Jul 1, 2021

724d80b this commit was added to the method that i changed, and there's also a couple of commits tagged against this ticket. We're still using 0.2.32, so haven't seen the issue yet.

@mashirozx
Copy link

Hi @thematan. Typeorm from version 0.2.12 support define default enum array as array of typescript enum #3414:

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

Not working now:

[Nest] 4856  - 12/30/2021, 2:08:13 PM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (5)...
QueryFailedError: ER_PARSE_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 'array ('user', 'admin') NOT NULL DEFAULT 'user'' at line 1

@Jbarget
Copy link

Jbarget commented Mar 10, 2023

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[];

@ngouy
Copy link

ngouy commented Feb 20, 2024

what worked for us was:

default: () => '{"value1", "value2"}'

@jonfreeland
Copy link

jonfreeland commented May 16, 2024

For a text array default value, we had to account for whether a value in the array had any spaces.

`'{${defaultValues.map((value) => (value.includes(" ") ? `"${value}"` : value)).join(",")}}'`;

PostgreSQL will strip unnecessary "s, which will cause the migration to be continually generated if you don't account for this.

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

Successfully merging a pull request may close this issue.