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

Bug on default values function? #4390

Open
spygi opened this issue Jul 4, 2019 · 5 comments
Open

Bug on default values function? #4390

spygi opened this issue Jul 4, 2019 · 5 comments

Comments

@spygi
Copy link

spygi commented Jul 4, 2019

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:
I believe there is an issue either with the default value functions (or I am missing something obvious).

I have a User entity with generated UUID following this comment (using the pgcrypto extension):

@Entity('users2')
export class User extends BaseEntity {
  @PrimaryColumn({
    type: 'uuid',
    default: () => 'gen_random_uuid()',
    nullable: false
  })
  public id: string | undefined;

I am trying to clear all users like this

createConnection({
    type: 'postgres',
    url: process.env.DATABASE_URL,
    synchronize: true,
    logging: true,
    uuidExtension: 'pgcrypto', // same behavior also without this
    entities: [
      User
    ]
}).then(async (connection: Connection) => {
    // clear all first
    await connection.getRepository(User).clear().then(() => {
      console.log('success');
    }).catch(async (err: Error) => {
      console.error(err);
      await connection.close();
      process.exit(1);
    });

    // insert one
    const user = new User();
    user.save();
});

Expected Outcome

Runs through successfully every time.

Actual Outcome

All goes well the first time I run this but every subsequent time I get the following error

DROP SEQUENCE "users2_id_seq"
query failed: DROP SEQUENCE "users2_id_seq"
error: { error: sequence "users2_id_seq" does not exist

Digging through the code I found the error is triggered in

upQueries.push(new Query(`DROP SEQUENCE ${this.buildSequenceName(table, newColumn)}`));
but not sure why we get there in the first place (isGenerated is true for oldColumn and false for newColumn although I changed nothing on the Entity between the 2 runs of the script)

I am using Postgres 11.3 on Mac.

@Ericnr
Copy link

Ericnr commented Jan 7, 2020

I'm having the same problem, Postgres 11.5 and typeorm 0.2.22.

@julianklumpers
Copy link

Same problem here. Postgres 9.6.10 with typeorm 0.2.22

@jeffstephens
Copy link

jeffstephens commented May 8, 2020

I'm also seeing this; seems to be related to the default UUID value as my other defaults and auto-incrementing columns don't exhibit this issue.

Postgres 11.4
TypeORM 0.2.24

As a workaround for primary columns, you can do this, at least:

@PrimaryGeneratedColumn('uuid')

@powersjcb
Copy link

Also ran into this issue. The suggestion provided by @jeffstephens worked for me.

@icrotz
Copy link

icrotz commented Jul 2, 2022

Hello, for those who also have the issue but doesn't want the "uuid" generated column to be a Primary Column you can also use this workaround:

@Column({
  default: () => "concat(uuid_generate_v4(), '')",
})
token: string;

Using the concat function will prevent TypeORM to think that this function is generated using uuid and remove an unexisting sequence

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

No branches or pull requests

8 participants