NestJS TypeORM:如果同步:true并且表存在,SQL错误

问题描述 投票:0回答:2

当我在 TypeORM 中启用同步和自动加载的情况下运行

npm run start
时,如果之前的
npm run start
中已经创建了表,则会返回以下
QueryFailedError

我别无选择,只能删除之前的所有表
npm run start

无论表的架构是否更新,如何启用
npm run start
并启用同步和自动加载?

error: Error: text type with an unknown/unsupported collation cannot be hashed
    at Packet.asError (/Users/myname/Documents/works/ski-backend/node_modules/mysql2/lib/packets/packet.js:728:17)
    at Query.execute (/Users/myname/Documents/works/ski-backend/node_modules/mysql2/lib/commands/command.js:29:26)
    at PoolConnection.handlePacket (/Users/myname/Documents/works/ski-backend/node_modules/mysql2/lib/connection.js:456:32)
    at PacketParser.onPacket (/Users/myname/Documents/works/ski-backend/node_modules/mysql2/lib/connection.js:85:12)
    at PacketParser.executeStart (/Users/myname/Documents/works/ski-backend/node_modules/mysql2/lib/packet_parser.js:75:16)
    at TLSSocket.<anonymous> (/Users/myname/Documents/works/ski-backend/node_modules/mysql2/lib/connection.js:360:25)
    at TLSSocket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at TLSSocket.Readable.push (node:internal/streams/readable:228:10) {
  code: 'ER_INTERNAL_ERROR',
  errno: 1815,
  sqlState: 'HY000',
  sqlMessage: 'text type with an unknown/unsupported collation cannot be hashed',
  sql: "SELECT `TABLE_SCHEMA`, `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA` = 'staging-myservice' AND `TABLE_NAME` = 'user' UNION SELECT `TABLE_SCHEMA`, `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA` = 'staging-myservice' AND `TABLE_NAME` = 'car'"
}

app.module.ts

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      url: 'mysql://<username>:<password>@<host>/<database>?ssl={"rejectUnauthorized":true}',
      synchronize: true,
      autoLoadEntities: true,
      logging: true,
      ssl: {
        rejectUnauthorized: true,
      },
    }),
  ]
})

用户.实体.ts

import { Column, PrimaryGeneratedColumn, Entity } from 'typeorm';
import { Field, ObjectType } from '@nestjs/graphql';

@Entity()
@ObjectType()
export class User {
  @PrimaryGeneratedColumn()
  @Field()
  id: number;

  @Column()
  @Field()
  name: string;
}

汽车.实体.ts

import { Column, PrimaryGeneratedColumn, Entity } from 'typeorm';
import { Field, ObjectType } from '@nestjs/graphql';

@Entity()
@ObjectType()
export class Car {
  @PrimaryGeneratedColumn()
  @Field()
  id: number;

  @Column()
  @Field()
  name: string;
}

我尝试了什么

  1. 明确指定表名。
    @Entity({ name: 'user' })
  2. 指定公共计划。
    @Entity({ name: 'user', schema: 'public' })
  3. 显式禁用迁移运行。
    migrationsRun: false
  4. 删除
    dist/
    目录。
  5. 安装
    mysql
    模块而不是
    mysql2
nestjs typeorm
2个回答
3
投票
synchronize - Indicates if database schema should be auto-created on every application launch. 
Be careful with this option and don't use this in production - otherwise, you can lose production data. 
This option is useful during debugging and development. As an alternative to it, you can use CLI and run schema:sync command. 
Note that for MongoDB database it does not create a schema, because MongoDB is schemaless. Instead, it syncs just by creating indices.

根据 TypeORM

docs
,这是 synchronise 的预期行为。

我认为对您来说最好的选择是根本不使用它。或者通过一些环境变量进行配置。


0
投票

我也有同样的问题。我通过将 migrationsRun 切换为 true 并同步为 false 来修复我的问题

@Module({
imports: [
    TypeOrmModule.forRootAsync({
  useFactory: async (
    ConfigService: ConfigService,
  ): Promise<TypeOrmModuleOptions> => ({
    type: 'mysql',
    host: ConfigService.getOrThrow('DB_HOST'),
    port: ConfigService.getOrThrow('DB_PORT'),
    username: ConfigService.getOrThrow('DB_USER'),
    password: ConfigService.getOrThrow('DB_PASSWORD'),
    database: ConfigService.getOrThrow('DB_NAME'),
    entities: [User],
    synchronize: false,
    migrationsRun: true, 
  }),
            inject: [ConfigService]
        }),
    ]
    }) 
    export class DatabaseModule {}
© www.soinside.com 2019 - 2024. All rights reserved.