node.js - 如何使用 TypeORM 迁移将计算数据插入数据库?
问题描述
我有一种情况,我需要在 TypeORM 中的现有表中添加一个新列。我将该列设置为“可为空”,因为表中存在需要添加该列的数据的现有项目,然后才能将该列设置为不可为空。
我一直在尝试编写一个迁移文件来将数据插入到表中每一行的新列中,但它似乎没有保存。
这就是我现在正在尝试的:
import { MigrationInterface, QueryRunner } from 'typeorm';
import { Customer } from '../../entities';
import { generateUrlName } from '../../entities/Customer';
export class fixUrlNameCustomers1584393813738 implements MigrationInterface {
name = 'fixUrlNameCustomers1584393813738';
public async up(queryRunner: QueryRunner): Promise<void> {
const customers = await queryRunner.manager.find(Customer);
const updatedCustomers = customers.map(customer => {
customer.url_name = generateUrlName(customer.name);
return customer;
});
await queryRunner.manager.save(updatedCustomers);
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}
如您所见,我正在尝试使用名为 generateUrlName 的函数为每个客户填写“.url-name”。在我运行此迁移并从 Postgres 执行查询后,我可以看到迁移没有保存数据。
解决方案
推荐阅读
- python - 如何使用GDAL rasterize(python API)按属性指定刻录值?
- r - r - 删除单个反斜杠
- python - 用于比较相关含义的短语和句子的算法
- typescript - browser.getProcessedConfig() 在詹金斯中不起作用
- r - lintr 无法在 RStudio 中加载
- python - Pandas,按最大返回 AssertionError 分组:
- javascript - JS:将数组转换为可读的 JSON 数组,每个值都有键对
- sql - SQL 返回最后一个空格右边的字符
- amazon-web-services - 模型 dynamodb 表?
- php - 用 foreach 循环上标签内的字符串替换特定值