为什么我收到此错误:迁移失败并出现错误:无法读取未定义的属性(读取“createTable”)

问题描述 投票:0回答:1

日志

npx knex 迁移:最新 迁移文件“20230917031623_createUsers.js”失败 迁移失败并出现错误:无法读取未定义的属性(读取“createTable”) 无法读取未定义的属性(读取“createTable”) 类型错误:无法读取未定义的属性(读取“createTable”) 在exports.up (C:\PROJECTS\ENVOLVIDOS_api\src\database\knex\migrations�30917031623_createUsers.js:1:35) 在 C:\PROJECTS\ENVOLVIDOS_api ode_modules\knex\lib\migrations\migrate\Migrator.js:519:40 在 process.processTicksAndRejections (节点:内部/进程/task_queues:95:5) 在 async Migrator._runBatch (C:\PROJECTS\ENVOLVIDOS_api ode_modules\knex\lib\migrations\migrate\Migrator.js:381:19)

knexfile.js

`const path = require("path");

module.exports = {
    client: 'mysql2',
    connection: {
        database : 'shopbarber',
        user: 'root',
        password: ''
    },
    pool: {
        min: 2,
        max: 10
    },
    migrations: {
        directory: path.resolve(__dirname, "src", "database", "knex", "migrations")
    }
}`

配置

`const config = require("../../../knexfile");
const knex = require("knex")(config);

knex.raw("SELECT VERSION()").then(() => {
    console.log('Connection to db succesful')
})

module.exports = knex;`

迁移文件

``exports.up = knex => knex.schemas.createTable("users", table => {
    table.increments("id");
    table.string("name", 60).notNullable();
    table.string("telephone", 11).notNullable();
    table.string("email", 90).notNullable().unique();
    table.text("password").notNullable()
    table.string("avatar", 120);
    table.timestamp("created_at").defaultTo(knex.fn.now());
    table.timestamp("updated_at").defaultTo(knex.fn.now());
})`

exports.down = knex => knex.schemas.dropTable("用户");`

我重新创建了迁移、文件、更改了功能,我不知道如何解决它。 连接已建立,我只是无法执行迁移。

帮助我?

javascript mysql node.js migration knex.js
1个回答
0
投票

错误告诉您

knex.schemas
未定义。这样做的原因是因为你有一个拼写错误,在你的
knex.schemas.createTable
中,
schemas
不应该有
s
。即,将其更改为
knex.schema.createTable
,它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.