mysql - 启动服务器后,带有sequelize的Nodejs无法在mysql工作台中创建表

标签 mysql node.js sequelize.js

我开始学习如何使用NodejsExpressjsSequelizeMySQL构建Rest API,使用Mysqlworkbench我的问题:启动服务器后,Sequelize 没有创建表,Mysqlworkbench 中也没有表。下面就出现了这条消息...提前感谢您的任何指导或帮助:-

Sequelize {
  options:
   { dialect: 'mysql',
     dialectModulePath: null,
     host: 'localhost',
     protocol: 'tcp',
     define: {},
     query: {},
     sync: {},
     timezone: '+00:00',
     logging: [Function: bound consoleCall],
     omitNull: false,
     native: false,
     replication: false,
     ssl: undefined,
     pool: {},
     quoteIdentifiers: true,
     hooks: {},
     retry: { max: 5, match: [Array] },
     transactionType: 'DEFERRED',
     isolationLevel: null,
     databaseVersion: 0,
     typeValidation: false,
     benchmark: false,
     operatorsAliases: false },
  config:
   { database: 'node',
     username: 'root',
     password: '1234567890',
     host: 'localhost',
     port: 3306,
     pool: {},
     protocol: 'tcp',
     native: false,
     ssl: undefined,
     replication: false,
     dialectModulePath: null,
     keepDefaultTimezone: undefined,
     dialectOptions: undefined },
  dialect:
   MysqlDialect {
     sequelize: [Circular],
     connectionManager:
      ConnectionManager {
        sequelize: [Circular],
        config: [Object],
        dialect: [Circular],
        versionPromise: null,
        dialectName: 'mysql',
        pool: [Object],
        lib: [Object] },
     QueryGenerator:
      { dialect: 'mysql',
        OperatorMap: [Object],
        createSchema: [Function: createSchema],
        showSchemasQuery: [Function: showSchemasQuery],
        versionQuery: [Function: versionQuery],
        createTableQuery: [Function: createTableQuery],
        showTablesQuery: [Function: showTablesQuery],
        addColumnQuery: [Function: addColumnQuery],
        removeColumnQuery: [Function: removeColumnQuery],
        changeColumnQuery: [Function: changeColumnQuery],
        renameColumnQuery: [Function: renameColumnQuery],
        handleSequelizeMethod: [Function: handleSequelizeMethod],
        _toJSONValue: [Function: _toJSONValue],
        upsertQuery: [Function: upsertQuery],
        deleteQuery: [Function: deleteQuery],
        showIndexesQuery: [Function: showIndexesQuery],
        showConstraintsQuery: [Function: showConstraintsQuery],
        removeIndexQuery: [Function: removeIndexQuery],
        attributeToSQL: [Function: attributeToSQL],
        attributesToSQL: [Function: attributesToSQL],
        quoteIdentifier: [Function: quoteIdentifier],
        _checkValidJsonStatement: [Function: _checkValidJsonStatement],
        jsonPathExtractionQuery: [Function: jsonPathExtractionQuery],
        _getForeignKeysQueryFields: [Function: _getForeignKeysQueryFields],
        getForeignKeysQuery: [Function: getForeignKeysQuery],
        getForeignKeyQuery: [Function: getForeignKeyQuery],
        dropForeignKeyQuery: [Function: dropForeignKeyQuery],
        _templateSettings: [Object],
        options: [Object],
        extractTableDetails: [Function: extractTableDetails],
        addSchema: [Function: addSchema],
        dropSchema: [Function: dropSchema],
        describeTableQuery: [Function: describeTableQuery],
        dropTableQuery: [Function: dropTableQuery],
        renameTableQuery: [Function: renameTableQuery],
        insertQuery: [Function: insertQuery],
        bulkInsertQuery: [Function: bulkInsertQuery],
        updateQuery: [Function: updateQuery],
        arithmeticQuery: [Function: arithmeticQuery],
        nameIndexes: [Function: nameIndexes],
        addIndexQuery: [Function: addIndexQuery],
        addConstraintQuery: [Function: addConstraintQuery],
        getConstraintSnippet: [Function: getConstraintSnippet],
        removeConstraintQuery: [Function: removeConstraintQuery],
        quoteTable: [Function: quoteTable],
        quote: [Function: quote],
        quoteIdentifiers: [Function: quoteIdentifiers],
        escape: [Function: escape],
        selectQuery: [Function: selectQuery],
        escapeAttributes: [Function: escapeAttributes],
        generateInclude: [Function: generateInclude],
        generateJoin: [Function: generateJoin],
        generateThroughJoin: [Function: generateThroughJoin],
        _generateSubQueryFilter: [Function: _generateSubQueryFilter],
        _getRequiredClosure: [Function: _getRequiredClosure],
        getQueryOrders: [Function: getQueryOrders],
        selectFromTableFragment: [Function: selectFromTableFragment],
        setAutocommitQuery: [Function: setAutocommitQuery],
        setIsolationLevelQuery: [Function: setIsolationLevelQuery],
        generateTransactionId: [Function: generateTransactionId],
        startTransactionQuery: [Function: startTransactionQuery],
        deferConstraintsQuery: [Function: deferConstraintsQuery],
        setConstraintQuery: [Function: setConstraintQuery],
        setDeferredQuery: [Function: setDeferredQuery],
        setImmediateQuery: [Function: setImmediateQuery],
        commitTransactionQuery: [Function: commitTransactionQuery],
        rollbackTransactionQuery: [Function: rollbackTransactionQuery],
        addLimitAndOffset: [Function: addLimitAndOffset],
        whereQuery: [Function: whereQuery],
        whereItemsQuery: [Function: whereItemsQuery],
        OperatorsAliasMap: false,
        setOperatorsAliases: [Function: setOperatorsAliases],
        whereItemQuery: [Function: whereItemQuery],
        _findField: [Function: _findField],
        _replaceAliases: [Function: _replaceAliases],
        _whereGroupBind: [Function: _whereGroupBind],
        _whereBind: [Function: _whereBind],
        _whereJSON: [Function: _whereJSON],
        _traverseJSON: [Function: _traverseJSON],
        _castKey: [Function: _castKey],
        _getJsonCast: [Function: _getJsonCast],
        _joinKeyValue: [Function: _joinKeyValue],
        _getSafeKey: [Function: _getSafeKey],
        _prefixKey: [Function: _prefixKey],
        _whereParseSingleValueObject: [Function: _whereParseSingleValueObject],
        getWhereConditions: [Function: getWhereConditions],
        parseConditionObject: [Function: parseConditionObject],
        isIdentifierQuoted: [Function: isIdentifierQuoted],
        booleanValue: [Function: booleanValue],
        _dialect: [Circular],
        sequelize: [Circular],
        typeValidation: undefined } },
  queryInterface:
   QueryInterface {
     sequelize: [Circular],
     QueryGenerator:
      { dialect: 'mysql',
        OperatorMap: [Object],
        createSchema: [Function: createSchema],
        showSchemasQuery: [Function: showSchemasQuery],
        versionQuery: [Function: versionQuery],
        createTableQuery: [Function: createTableQuery],
        showTablesQuery: [Function: showTablesQuery],
        addColumnQuery: [Function: addColumnQuery],
        removeColumnQuery: [Function: removeColumnQuery],
        changeColumnQuery: [Function: changeColumnQuery],
        renameColumnQuery: [Function: renameColumnQuery],
        handleSequelizeMethod: [Function: handleSequelizeMethod],
        _toJSONValue: [Function: _toJSONValue],
        upsertQuery: [Function: upsertQuery],
        deleteQuery: [Function: deleteQuery],
        showIndexesQuery: [Function: showIndexesQuery],
        showConstraintsQuery: [Function: showConstraintsQuery],
        removeIndexQuery: [Function: removeIndexQuery],
        attributeToSQL: [Function: attributeToSQL],
        attributesToSQL: [Function: attributesToSQL],
        quoteIdentifier: [Function: quoteIdentifier],
        _checkValidJsonStatement: [Function: _checkValidJsonStatement],
        jsonPathExtractionQuery: [Function: jsonPathExtractionQuery],
        _getForeignKeysQueryFields: [Function: _getForeignKeysQueryFields],
        getForeignKeysQuery: [Function: getForeignKeysQuery],
        getForeignKeyQuery: [Function: getForeignKeyQuery],
        dropForeignKeyQuery: [Function: dropForeignKeyQuery],
        _templateSettings: [Object],
        options: [Object],
        extractTableDetails: [Function: extractTableDetails],
        addSchema: [Function: addSchema],
        dropSchema: [Function: dropSchema],
        describeTableQuery: [Function: describeTableQuery],
        dropTableQuery: [Function: dropTableQuery],
        renameTableQuery: [Function: renameTableQuery],
        insertQuery: [Function: insertQuery],
        bulkInsertQuery: [Function: bulkInsertQuery],
        updateQuery: [Function: updateQuery],
        arithmeticQuery: [Function: arithmeticQuery],
        nameIndexes: [Function: nameIndexes],
        addIndexQuery: [Function: addIndexQuery],
        addConstraintQuery: [Function: addConstraintQuery],
        getConstraintSnippet: [Function: getConstraintSnippet],
        removeConstraintQuery: [Function: removeConstraintQuery],
        quoteTable: [Function: quoteTable],
        quote: [Function: quote],
        quoteIdentifiers: [Function: quoteIdentifiers],
        escape: [Function: escape],
        selectQuery: [Function: selectQuery],
        escapeAttributes: [Function: escapeAttributes],
        generateInclude: [Function: generateInclude],
        generateJoin: [Function: generateJoin],
        generateThroughJoin: [Function: generateThroughJoin],
        _generateSubQueryFilter: [Function: _generateSubQueryFilter],
        _getRequiredClosure: [Function: _getRequiredClosure],
        getQueryOrders: [Function: getQueryOrders],
        selectFromTableFragment: [Function: selectFromTableFragment],
        setAutocommitQuery: [Function: setAutocommitQuery],
        setIsolationLevelQuery: [Function: setIsolationLevelQuery],
        generateTransactionId: [Function: generateTransactionId],
        startTransactionQuery: [Function: startTransactionQuery],
        deferConstraintsQuery: [Function: deferConstraintsQuery],
        setConstraintQuery: [Function: setConstraintQuery],
        setDeferredQuery: [Function: setDeferredQuery],
        setImmediateQuery: [Function: setImmediateQuery],
        commitTransactionQuery: [Function: commitTransactionQuery],
        rollbackTransactionQuery: [Function: rollbackTransactionQuery],
        addLimitAndOffset: [Function: addLimitAndOffset],
        whereQuery: [Function: whereQuery],
        whereItemsQuery: [Function: whereItemsQuery],
        OperatorsAliasMap: false,
        setOperatorsAliases: [Function: setOperatorsAliases],
        whereItemQuery: [Function: whereItemQuery],
        _findField: [Function: _findField],
        _replaceAliases: [Function: _replaceAliases],
        _whereGroupBind: [Function: _whereGroupBind],
        _whereBind: [Function: _whereBind],
        _whereJSON: [Function: _whereJSON],
        _traverseJSON: [Function: _traverseJSON],
        _castKey: [Function: _castKey],
        _getJsonCast: [Function: _getJsonCast],
        _joinKeyValue: [Function: _joinKeyValue],
        _getSafeKey: [Function: _getSafeKey],
        _prefixKey: [Function: _prefixKey],
        _whereParseSingleValueObject: [Function: _whereParseSingleValueObject],
        getWhereConditions: [Function: getWhereConditions],
        parseConditionObject: [Function: parseConditionObject],
        isIdentifierQuoted: [Function: isIdentifierQuoted],
        booleanValue: [Function: booleanValue],
        _dialect: [Object],
        sequelize: [Circular],
        typeValidation: undefined } },
  models: {},
  modelManager: ModelManager { models: [], sequelize: [Circular] },
  connectionManager:
   ConnectionManager {
     sequelize: [Circular],
     config:
      { database: 'node',
        username: 'root',
        password: '1234567890',
        host: 'localhost',
        port: undefined,
        pool: [Object],
        protocol: 'tcp',
        native: false,
        ssl: undefined,
        replication: false,
        dialectModulePath: null,
        keepDefaultTimezone: undefined,
        dialectOptions: undefined },
     dialect:
      MysqlDialect {
        sequelize: [Circular],
        connectionManager: [Circular],
        QueryGenerator: [Object] },
     versionPromise: null,
     dialectName: 'mysql',
     pool:
      Pool {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        _config: [Object],
        _Promise: [Object],
        _factory: [Object],
        _draining: false,
        _started: false,
        _waitingClientsQueue: [Object],
        _factoryCreateOperations: Set {},
        _factoryDestroyOperations: Set {},
        _availableObjects: [Object],
        _testOnBorrowResources: Set {},
        _testOnReturnResources: Set {},
        _validationOperations: Set {},
        _allObjects: Set {},
        _resourceLoans: Map {},
        _evictionIterator: [Object],
        _evictor: DefaultEvictor {},
        _scheduledEviction: null },
     lib:
      { createConnection: [Function],
        connect: [Function],
        Connection: [Function: Connection],
        createPool: [Function],
        createPoolCluster: [Function],
        createQuery: [Function: createQuery],
        Pool: [Function: Pool],
        createServer: [Function],
        PoolConnection: [Object],
        escape: [Function: escape],
        escapeId: [Function: escapeId],
        format: [Function: format],
        raw: [Function: raw],
        createConnectionPromise: [Getter],
        createPoolPromise: [Getter],
        createPoolClusterPromise: [Getter],
        Types: [Getter],
        Charsets: [Getter],
        CharsetToEncoding: [Getter],
        setMaxParserCache: [Function],
        clearParserCache: [Function] } },
  importCache: {},
  test:
   { _trackRunningQueries: false,
     _runningQueries: 0,
     trackRunningQueries: [Function: trackRunningQueries],
     verifyNoRunningQueries: [Function: verifyNoRunningQueries] } }

模型product.js

const Sequelize = require('sequelize');

const sequelize = require('./dbconfig');


const Product = sequelize.define('product', {
  id: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true
  },
  title: Sequelize.STRING,
  price: {
    type: Sequelize.DOUBLE,
    allowNull: false
  },
  imageUrl: {
    type: Sequelize.STRING,
    allowNull: false
  },
  description: {
    type: Sequelize.STRING,
    allowNull: false
  }
});

module.exports = Product;

App.js

const express = require('express');
const feedroutes = require('./routes/feed');
const bodyParser = require('body-parser');
const sequelize = require('./dbconfig');


const app = express()

app.use(bodyParser.json());//application/json

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
});

app.use('/feed',feedroutes);

sequelize.sync().then(reslut => {
    console.log(reslut);
    app.listen(8080); 

})
.catch(error => {
    console.log(error);   
});

dbconfig.js

const Sequelize = require('sequelize');

const sequelize = new Sequelize('node','root','1234567890', { 
    operatorsAliases: false ,
    dialect: 'mysql',
    host:'localhost'

});


module.exports = sequelize;

最佳答案

由于您的product模型位于单独的文件中,因此您需要使用sequelize实例上可用的import方法导入它,如下所示

模型product.js

module.exports = function(sequelize, Sequelize) {
    const Product = sequelize.define('product', {
        id: {
            type: Sequelize.INTEGER,
            autoIncrement: true,
            allowNull: false,
            primaryKey: true
        },
        title: Sequelize.STRING,
        price: {
            type: Sequelize.DOUBLE,
            allowNull: false
        },
        imageUrl: {
            type: Sequelize.STRING,
            allowNull: false
        },
        description: {
            type: Sequelize.STRING,
            allowNull: false
        }
    });

    module.exports = Product;
};

dbconfig.js

const Sequelize = require('sequelize');

const sequelize = new Sequelize('node','root','1234567890', { 
    operatorsAliases: false ,
    dialect: 'mysql',
    host:'localhost'

});

sequelize.import('path/to/product/model');

module.exports = sequelize;

引用sequelize import

项目Demo

关于mysql - 启动服务器后,带有sequelize的Nodejs无法在mysql工作台中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54957159/

相关文章:

php - mysql_connect 错误 500

mysql - 如何在 MySQL 中将子字符串替换为列值?

php - 想创建一个问答网站,那么我应该阅读什么来构建这样的网站?

javascript - 从nodejs中的多部分请求中检索JSON对象

node.js - Sequelize选择不同的行

mysql - Sequelize - 从关联表中返回单个列作为自定义列

sequelize.js - 如何将varchar列与sequ​​elize中日期的时间部分进行比较

php - 注意 : Undefined variable: table in C:\XAMPP\htdocs\progscores. php 第 27 行

node.js - Google Cloud Services 中 f1 微型实例中 node.js 进程的进程内存限制?

javascript - 更新 mongodb 中的数组