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

Update to 0.2.5 breaking things #2108

Closed
younusmahmood opened this issue May 9, 2018 · 16 comments
Closed

Update to 0.2.5 breaking things #2108

younusmahmood opened this issue May 9, 2018 · 16 comments

Comments

@younusmahmood
Copy link

younusmahmood commented May 9, 2018

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

@younusmahmood
Copy link
Author

younusmahmood commented May 9, 2018

I recently updated from 0.2.0-alpha.27 to 0.2.5 and suddenly everything is breaking. its hard to reproduce the code but I can give an example:

@Entity()
export class VariableMethod {
  @PrimaryColumn()
  name: string;

  @Column() label: string;
}

This was working fine as the server started but now after the update it gives me
column "name" contains null values

I then added a default value

@Entity()
export class VariableMethod {
  @PrimaryColumn({ default: 'test'})
  name: string;

  @Column() label: string;
}

Then it tells me that the name test is duplicated and

Error: could not create unique index "PK_23423423" 
messageDetail: 'Key (name)=(test) is duplicated.',

Any idea on why this is suddenly happening?

@younusmahmood younusmahmood changed the title Update to 0.2.5 breaking a whole lot of stuff Update to 0.2.5 breaking things May 9, 2018
@pleerock
Copy link
Member

pleerock commented May 9, 2018

its a primary column and primary columns are not allowed to have duplicated values, so please check your data.

@younusmahmood
Copy link
Author

Why doesn't that error show up in the older versions then? And it drops it right before adding it again.

query: ALTER TABLE "variable_method" DROP CONSTRAINT "PK_23423423"
query: ALTER TABLE "variable_method" DROP COLUMN "name"
query: ALTER TABLE "variable_method" ADD "name" character varying NOT NULL DEFAULT 'test'
query: ALTER TABLE "variable_method" ADD CONSTRAINT "PK_23423423" PRIMARY KEY ("name")
query failed: ALTER TABLE "variable_method" ADD CONSTRAINT "PK_23423423" PRIMARY KEY ("name")
error: { error: could not create unique index "PK_23423423"

@pleerock
Copy link
Member

pleerock commented May 9, 2018

Why doesn't that error show up in the older versions then?

don't know, maybe there was a bug and something was working wrong in previous versions

And it drops it right before adding it again.

correct. primary columns are required to be dropped to make some changes on them.

@younusmahmood
Copy link
Author

Got it, thanks.

@RDeluxe
Copy link

RDeluxe commented May 31, 2018

Not sure if I should be opening a new issue or ask to reopen this one, I'm encountering the same behavior.

import { Entity, Column, PrimaryGeneratedColumn, BaseEntity } from 'typeorm';
import * as slug from 'slug';

@Entity()
export class SkillTagEntity extends BaseEntity {
  @PrimaryGeneratedColumn('uuid') 
  public id: string;
  
  @Column('varchar', { unique: true })
  public slug: string;
  
  @Column('varchar') 
   private name: string;

  @Column('timestamp', { nullable: true })
  public deleteDate?: Date;
}

The error :

ERROR:  column "slug" contains null values
STATEMENT:  ALTER TABLE "skill_tag" ADD "slug" character varying NOT NULL
column "slug" contains null values
QueryFailedError: column "slug" contains null values

Thing is, I have no null value in the column "slug", not do I have duplicates.
I checked the existence of duplicates with the following command :

select * from skill_tag tag
where (select count(*) from skill_tag inr
where inr.slug = tag.slug) > 1

Seems to me that the combo NOT NULL + UNIQUE, which is preventing us to use a default value, is causing this bug.

Edit : it seems that it's trying to add the column instead of altering it ? But the error does not concur

@Alain1405
Copy link

@RDeluxe I had the same error popping up when the data in the tables looked perfect. Your comment about the query actually trying to create the column helped me find the issue. Do you have synchronize: true in your ormconfig.json? If so, every time you run the app typeorm tries to recreate the tables and since you have data in there, throws that misleading error.
@pleerock should I report it as an issue?

@Tolga-FNT
Copy link

@younusmahmood

Please re-open the issue, because it seems that the devs here don't take this issue serious. I am having the same problem with @Alain1405 @RDeluxe.

@fullofcaffeine
Copy link

Same problem here.

@Tolga-FNT
Copy link

Same problem here.

To solve this issue, delete the entire dist folder and try again. Hopefully it works for you as well. Let us know if this did the trick.

@JairLopesJunior
Copy link

I was having the same problem, I noticed that in PGAdmin the fields that gave this error were with the type "VARING CHARACTER" without the length.

Ex: I had a field in my Entity called crp which is of the string type which is six characters, but in the Database it was only "CHARACTER VARYING" I changed it to "CHARACTER VARYING (6)" and it worked, I hope it helps someone :D

@shcallaway
Copy link

shcallaway commented Feb 11, 2022

I'm having a similar issue when trying to change the type of a column.

Here's the migration that creates the table originally:

import { MigrationInterface, QueryRunner, Table } from "typeorm";

export class CreateClient1644456282318 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        name: "client",
        columns: [
          {
            name: "id",
            type: "uuid",
            default: "uuid_generate_v4()",
            isPrimary: true,
          },
          {
            name: "name",
            type: "varchar",
            length: "100",
          },
          {
            name: "created_at",
            type: "timestamptz",
            default: "now()",
          },
        ],
      }),
      true
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("client");
  }
}

Here's the migration that alters the column type:

import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";

export class ChangeClientNameColumnType1644540433176
  implements MigrationInterface
{
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.changeColumn(
      "client",
      "name",
      new TableColumn({
        name: "name",
        type: "text",
      })
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.changeColumn(
      "client",
      "name",
      new TableColumn({
        name: "name",
        type: "varchar",
        length: "100",
      })
    );
  }
}

When I run start my app in production, it crashes on this migration:

yarn run v1.22.17
--
$ node ./dist/index.js
Using prod connection options
3 migrations are already loaded in the database.
4 migrations were found in the source code.
CreateToken1644456290388 is the last executed migration. It was executed on Thu Feb 10 2022 01:24:50 GMT+0000 (Coordinated Universal Time).
1 migrations are new migrations that needs to be executed.
Migration "ChangeClientNameColumnType1644540433176" has been failed, error: column "name" of relation "client" contains null values
/public-api/node_modules/typeorm/error/TypeORMError.js:9
var _this = _super.call(this, message) \|\| this;
^
QueryFailedError: column "name" of relation "client" contains null values
at QueryFailedError.TypeORMError [as constructor] (/public-api/node_modules/typeorm/error/TypeORMError.js:9:28)
at new QueryFailedError (/public-api/node_modules/typeorm/error/QueryFailedError.js:13:28)
at PostgresQueryRunner.<anonymous> (/public-api/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:250:31)
at step (/public-api/node_modules/tslib/tslib.js:143:27)
at Object.throw (/public-api/node_modules/tslib/tslib.js:124:57)
at rejected (/public-api/node_modules/tslib/tslib.js:115:69)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
query: 'ALTER TABLE "client" ADD "name" text NOT NULL',
parameters: undefined,
driverError: error: column "name" of relation "client" contains null values
at Parser.parseErrorMessage (/public-api/node_modules/pg-protocol/dist/parser.js:287:98)
at Parser.handlePacket (/public-api/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/public-api/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/public-api/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 140,
severity: 'ERROR',
code: '23502',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'client',
column: 'name',
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '5454',
routine: 'ATRewriteTable'
},
length: 140,
severity: 'ERROR',
code: '23502',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'client',
column: 'name',
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '5454',
routine: 'ATRewriteTable'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I checked the database, and the client.name column does NOT contain any null values...

This is not caused by synchronize: true. Here are my connection options:

import { ConnectionOptions, createConnection } from "typeorm";

const baseConnOpts: ConnectionOptions = {
  type: "postgres",
  port: 5432,
  entities: [__dirname + "/entity/**/*.js"],
  migrations: [__dirname + "/migrations/**/*.js"],
  migrationsRun: true,
  synchronize: false,
  logging: ["schema"],
  logger: "simple-console",
};

// ...

const prodConnOpts: ConnectionOptions = {
  ...baseConnOpts,
  ...{
    host: process.env.DB_HOST,
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
  },
};

function getConnOpts() {
  if (process.env.ENV == "prod") {
    console.log("Using prod connection options");
    return prodConnOpts;
  } else {
    console.log("Using dev connection options");
    return devConnOpts;
  }
}

export function getConnection() {
  return createConnection(getConnOpts());
}

@andresogando
Copy link

I had this issue and it can be fixed indeed as the community mentioned by setting Synchronize: false but also it can happen if you try to save data that has any type of relationship before saving its corresponding relation. e.g: Category and Question, one question has multiple categories so you try to save the question without saving first the categories if they don't exist already in the database.

@Pedro-Marques-Santos
Copy link

Pedro-Marques-Santos commented Feb 2, 2023

I had the same bug, when I was going to add a column in the "user" table the solution was to warn that initially the column will be null, unfortunately I could not add a fixed value when creating it, as it generated some errors. Here's the example that worked for me:

import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";

export class addColumnMotivationTableUser1675347094874
  implements MigrationInterface
{
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.addColumn(
      "user",
      new TableColumn({
        name: "motivation",
        type: "varchar",
        isNullable: true,
      })
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropColumn("user", "motivation");
  }
}

@iyunusov
Copy link

iyunusov commented Feb 26, 2023

What's up with this issue now? it's still happening.
I got a table with data called categories there's no null data in it in none of the columns, it says:

      [Nest] 11761  - 02/26/2023, 1:46:04 PM   ERROR [ExceptionHandler] column "name" of relation "categories" contains null values
      QueryFailedError: column "name" of relation "categories" contains null values

I tried doing this in the category.entity class:

     @Column({ nullable: true })
      name: string;

It auto updated all name strings to NULL, stupid feature isn't it?
Setting syncronize: false is solving it, but in production won't it throw the same error when matching the entity to table with content? Also, why shall we turn off syncronization if that feature is there?

@iyunusov
Copy link

iyunusov commented Mar 11, 2023

Giving @Column a type property that is set to text fixed the issue, like this:

@Entity({ name: 'categories' })
export class Category {
  @Column({ type: 'text' })
  name: string;
   // Other columns...
}

Now I keep the synchronize: true in ormconfig.json as well and data isn't lost.

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

No branches or pull requests