简体   繁体   English

Knex.js 迁移问题:因`关系“knex_migrations”不存在而失败`

[英]Knex.js migration issue: Fails with ` relation "knex_migrations" does not exist`

I've been using knex.js successfully for a while now on this project, first time I've come accross this.我已经在这个项目上成功使用了一段时间的 knex.js,这是我第一次遇到这个。 I deleted all my migration files and dropped my db (locally) and instead used pg_dump to get the DDL and all data from my prod database (not including knex_migrations or knex_migrations_lock tables).我删除了所有迁移文件并删除了我的数据库(本地),而是使用 pg_dump 从我的 prod 数据库(不包括 knex_migrations 或 knex_migrations_lock 表)中获取 DDL 和所有数据。

I created a new migration script and used knex.raw to paste all the SQL in. When I ran the migration script, I get the following:我创建了一个新的迁移脚本并使用knex.raw将所有 SQL 粘贴到其中。当我运行迁移脚本时,我得到以下信息:

error: insert into "knex_migrations" ("batch", "migration_time", "name") values ($1, $2, $3) - relation "knex_migrations" does not exist

This migration script is being run on a brand new database.此迁移脚本正在一个全新的数据库上运行。 When I change what's in the script to just be basic DDL, it works fine.当我将脚本中的内容更改为基本 DDL 时,它可以正常工作。 So something about the result of pg_dump and trying to run it using knex is causing it to bomb out.因此,关于 pg_dump 的结果并尝试使用 knex 运行它会导致它被炸毁。

This isn't a ton of data either really, so I'm not too sure where I'm going wrong - knex is responsible for creating the migration tables and I've made sure there's no mention of knex or migration tables in the DDL.这也不是大量数据,所以我不太确定哪里出错了 - knex 负责创建迁移表,我确保 DDL 中没有提及 knex 或迁移表.

Any suggestions would be greatly appreciated:)任何建议将不胜感激:)

The table knex_migrations and knex_migrations_lock will be created on running the command运行命令时将创建表knex_migrationsknex_migrations_lock

knex migrate:make <name of migration>

Could you please confirm you have executed above knex command.您能否确认您已执行上述 knex 命令。

Once that is done running一旦完成运行

knex migrate:latest

should create all schema you have mention.应该创建您提到的所有架构。 knex.raw knex.raw

Using pd_dump to dump a database to an sql file, there occurs some additional lines in the beginning of the sql such as:使用 pd_dump 将数据库转储到 sql 文件中,在 sql 的开头出现了一些额外的行,例如:

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

comment those out and the migration should work.将它们注释掉,迁移应该可以工作。 You can uncomment one statement at a time to see which one is causing the problem.您可以一次取消注释一个语句,以查看导致问题的语句。

I just went down this hole.我刚进了这个洞。 The following line is the culprit.以下行是罪魁祸首。

SELECT pg_catalog.set_config('search_path', '', false);

It's removing your search path so knex doesn't know where to find anything.它正在删除您的搜索路径,因此 knex 不知道在哪里可以找到任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM