Creating Migrations using module.Exports

Asked

Viewed 196 times

0

Hello, I’m trying to create a Migration using the knex, but the knex does not accept the export default then as an alternative I am using the import mode ECS5 (ex: const knex = require('knex) and module.exports...). However when trying to create Migration using the command npx knex migrate:latest it just returns in the console the message Already up to date but while trying to visualize the table using vs code extension SQLite it does not show the table that theoretically was to have created

Well, I’d like to know what I’m doing wrong, I’ve tried several ways, I’ve tried to set up for the knex to accept ECS6 but I didn’t succeed, if you can help me I’d be grateful !

File of my Migration

const knex = require("knex");

module.exports = async function up(knex) {
  return knex.schema.createTable("help", function (table) {
    table.increments("id").primary();
    table.string("name").notNullable();
    table.string("email").notNullable();
    table.string("phone").notNullable();
    table.string("city").notNullable();
    table.string("uf").notNullable();
    table.string("neighborhood").notNullable();
    table.string("description").notNullable();
  });
};

module.exports = async function down(knex) {
  return knex.schema.dropTable("help");
};

knexfile.js (yes in root)

const path = require("path");
module.exports = {
  client: "sqlite3",
  connection: {
    filename: path.resolve(__dirname, "src", "database", "database.sqlite"),
  },

  migrations: {
    directory: path.resolve(__dirname, "src", "database", "migrations"),
},
    useNullAsDefault: true,
};

File connection to the bank

const knex = require("knex");
import path from "path";

exports.connection = knex({
  client: "sqlite3",
  connection: {
    filename: path.resolve(__dirname, "database.sqlite"),
  },
});

extensão SQLITE do vscode

1 answer

0

the export default should be used when Voce only export a single value, function, object. see here.
what perhaps Voce should do, or try to, use the export. for the methods up and down, and put the await right after the return. in this way:

exports.up = async function(knex, Promise) {
 return await knex.schema.createTable('tasks', function(table) {
  // ....
 })
}

exports.down = async function(knex, Promise) {
 return await knex.schema.dropTable('tasks');
}

Tried this way?

  • Hello, I had already tried this way, but I tried once again just to be sure and it comes back to what I had said, it returns only "Already up to date" but does not create the table in the bank, when I try to visualize it with the SQLITE extension it is empty.

  • Well, in that case, I suggest you delete those Migrations tables from the knex (knex_migrations and knex_migrations_lock), because they have all Migrations logs executed. While running your Migrations again (without Voce having given rollback in them), if you have any record in these tables about the Migration you want to run, the knex will assume that the Migration has already been run and then you would need to run it again. Maybe that’s why your tables don’t show up. Try the test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.