在我的Node项目中,我使用knex和postgres,我有两个不同的项目使用相同的数据库,但使用不同的模式。我可以使用不同的模式操作,同时编写任何查询或使用
knex.schema.withSchema,但是knex并没有在这些模式中创建锁表。它使用数据库中的默认公共架构来创建锁表。因此,第一个项目可以很好地迁移,但当我迁移第二个项目时,它会引发错误-错误:迁移目录已损坏,缺少以下文件: 20210430124918_test.ts
这是第一个项目的文件。因为它使用相同名称的公共架构来创建锁表,所以它认为文件丢失了。
我不想在knexfile.ts中更改迁移锁文件名,是否有一种方法可以指定用于为不同项目创建锁表的模式。
提前谢谢。
发布于 2021-07-28 19:47:46
对于不同的项目,您需要指定一个单独的knexfile,它告诉迁移要使用的正确的迁移目录和正确的模式名称。
https://knexjs.org/#Migrations-API
不过,只为两个projejct使用完全独立的数据库要容易得多。
发布于 2022-05-22 06:21:38
我也有同样的问题,如果您想通过代码本身来处理它--更新代码--有点像
const migrationConfig = {
directory: this.migrationDirectory,
tableName: 'knex_migration_reporter', //check here I changed the name
};
let q = await this.dbClient.client().migrate.latest(migrationConfig);
如果您检查类型文件,您可以看到它接受的接口。
interface MigratorConfig {
database?: string;
directory?: string | readonly string[];
extension?: string;
stub?: string;
tableName?: string;
schemaName?: string;
disableTransactions?: boolean;
disableMigrationsListValidation?: boolean;
sortDirsSeparately?: boolean;
loadExtensions?: readonly string[];
migrationSource?: MigrationSource<unknown>;
name?: string;
}
因此,您可以在相同的模式中提供不同的模式名称或不同的knex表,不管您喜欢什么。
https://stackoverflow.com/questions/67334847
复制相似问题