TypeORM 和 SQL 服务器 - 在主键上设置“NONCLUSTERED”和“NOT ENFORCED”

TypeORM and SQL Server - Set `NONCLUSTERED` and `NOT ENFORCED` on primary key

问题:如何在 SQL 服务器中使用 NONCLUSTEREDNOT ENFORCED 配置 TypeORM 实体的主列?

上下文

我正在尝试建立与 Azure 上 SQL 服务器实例的连接,以便与他们的 Synapse 服务一起使用。既然如此,连接有very specific requirements for the primary key (more details here),主要是我需要在我的主键上设置NONCLUSTEREDNOT ENFORCED没有主键.

由于 TypeORM 不接受没有主键的实体,我想配置主键以满足 Synapse SQL 服务器的要求。

像这样配置我的实体时:

@Entity()
export class BlenderEvent {
  @PrimaryGeneratedColumn()
  public id: number;
}

我得到:

[Nest] 1165402   - 2021-10-01, 3:35:15 p.m.   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +3524ms
QueryFailedError: Error: Enforced unique constraints are not supported. To create an unenforced unique constraint you must include the NOT ENFORCED syntax as part of your statement.
    at QueryFailedError.TypeORMError [as constructor] (/home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/typeorm/error/TypeORMError.js:9:28)
    at new QueryFailedError (/home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/typeorm/error/QueryFailedError.js:13:28)
    at /home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/typeorm/driver/sqlserver/SqlServerQueryRunner.js:281:46
    at /home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/mssql/lib/base/request.js:438:25
    at Request.userCallback (/home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/mssql/lib/tedious/request.js:493:15)
    at Request.callback (/home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/tedious/lib/request.js:205:14)
    at Parser.onEndOfMessage (/home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/tedious/lib/connection.js:3078:22)
    at Object.onceWrapper (node:events:509:28)
    at Parser.emit (node:events:390:28)
    at Readable.<anonymous> (/home/gcardinal/dev/casino/mssql-nestjs-poc/node_modules/tedious/lib/token/token-stream-parser.js:34:12)

如果我使用 @PrimaryColumn() 而不是 @PrimaryGeneratedColumn(),则相同。

我怎样才能做到这一点?

你不知道。在这种情况下,最好的做法是在您的 TypeORM 选项中使用 synchronize: false 来防止 TypeORM 在您的数据库中创建或修改表。它将阻止它抱怨缺少主键。

如果您想将基础设施作为数据库的代码(就像我的情况一样),您的微服务不适合这样做。

感谢@David Browne 在对我的问题的评论中提出的建议。