为什么console.log在我没有调用它时显示一个CREATE TABLE语句

时间:2022-03-12 13:41:02

Code

require('dotenv/config');
const Sequelize = require('sequelize');

const connection = new Sequelize(process.env.DATABASE, process.env.USER, process.env.PASSWORD, {
    host: process.env.HOST,
    dialect: 'mysql',
    operatorsAliases: false,

    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    },

});

var Article = connection.define('article', {
    title: Sequelize.STRING,
    body: Sequelize.TEXT
});

connection.sync()
    .then(function () {
        // Article.create({
        //     title: 'demo title',
        //     body: 'Blah'
        // });
        Article.findById(1).then(function (article) {
            console.log(article.dataValues);
        });
    });
    // .then(function () { process.exit(); });

console.log

的console.log

PS C:\code\basic-sequelize> npm start

> basic-sequelize@1.0.0 start C:\code\basic-sequelize
> node app.js

Executing (default): CREATE TABLE IF NOT EXISTS `articles` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255), `body` TEXT,
`createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `articles`
Executing (default): SELECT `id`, `title`, `body`, `createdAt`, `updatedAt` FROM `articles` AS `article` WHERE `article`.`id` = 1;
{ id: 1,
  title: 'demo title',
  body: 'Blah',
  createdAt: 2018-06-12T17:07:21.000Z,
  updatedAt: 2018-06-12T17:07:21.000Z }

2 个解决方案

#1


1  

Those are calls from Sequelize. You can disable those if you like with logging: false,

这些是来自Sequelize的电话。如果您喜欢使用日志记录,可以禁用它们:false,

const connection = new Sequelize(process.env.DATABASE, process.env.USER, process.env.PASSWORD, {
    host: process.env.HOST,
    dialect: 'mysql',
    operatorsAliases: false,
    logging: false, // <--- Disable logging
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    },

});

#2


1  

That is the expected behavior of Sequelize.

这是Sequelize的预期行为。

You asked for it via the .sync() call.

您通过.sync()调用询问了它。

From the docs:

来自文档:

When starting a new project you won't have a database structure and using Sequelize you won't need to. Just specify your model structures and let the library do the rest. Currently supported is the creation and deletion of tables.

在开始一个新项目时,你将没有数据库结构,并且你不需要使用Sequelize。只需指定您的模型结构,让库完成其余的工作。目前支持的是创建和删除表。

#1


1  

Those are calls from Sequelize. You can disable those if you like with logging: false,

这些是来自Sequelize的电话。如果您喜欢使用日志记录,可以禁用它们:false,

const connection = new Sequelize(process.env.DATABASE, process.env.USER, process.env.PASSWORD, {
    host: process.env.HOST,
    dialect: 'mysql',
    operatorsAliases: false,
    logging: false, // <--- Disable logging
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    },

});

#2


1  

That is the expected behavior of Sequelize.

这是Sequelize的预期行为。

You asked for it via the .sync() call.

您通过.sync()调用询问了它。

From the docs:

来自文档:

When starting a new project you won't have a database structure and using Sequelize you won't need to. Just specify your model structures and let the library do the rest. Currently supported is the creation and deletion of tables.

在开始一个新项目时,你将没有数据库结构,并且你不需要使用Sequelize。只需指定您的模型结构,让库完成其余的工作。目前支持的是创建和删除表。