SequelizeDatabaseError:列不存在(PostgreSQL)

时间:2018-06-27 09:38:23

标签: postgresql express sequelize.js

我将Sequelize与Postgresql DB和ExpressJS一起使用。

有2个模型ManufacturerManufacturerTab。这两个模型相互关联。 Manufacturer具有许多ManufacturerTabManufacturerTab属于Manufacturer。 尝试获取ManufacturerTabs列表时出现错误:

{
    "name": "SequelizeDatabaseError",
    "message": "column ManufacturerTabs.ManufacturerId does not exist",
    "parent": {
        "name": "error",
        "length": 130,
        "severity": "ERROR",
        "code": "42703",
        "position": "704",
        "file": "parse_relation.c",
        "line": "3293",
        "routine": "errorMissingColumn",
        "sql": "SELECT \"Manufacturer\".\"id\", \"Manufacturer\".\"manufacturer_name\", \"Manufacturer\".\"manufacturer_logo_url\", \"Manufacturer\".\"manufacturer_archived_status\", \"Manufacturer\".\"createdAt\", \"Manufacturer\".\"updatedAt\", \"ManufacturerTabs\".\"id\" AS \"ManufacturerTabs.id\", \"ManufacturerTabs\".\"sequence\" AS \"ManufacturerTabs.sequence\", \"ManufacturerTabs\".\"tab_name\" AS \"ManufacturerTabs.tab_name\", \"ManufacturerTabs\".\"createdAt\" AS \"ManufacturerTabs.createdAt\", \"ManufacturerTabs\".\"updatedAt\" AS \"ManufacturerTabs.updatedAt\", \"ManufacturerTabs\".\"ManufacturerId\" AS \"ManufacturerTabs.ManufacturerId\" FROM \"Manufacturers\" AS \"Manufacturer\" LEFT OUTER JOIN \"ManufacturerTabs\" AS \"ManufacturerTabs\" ON \"Manufacturer\".\"id\" = \"ManufacturerTabs\".\"ManufacturerId\";"
    },
    "original": {
        "name": "error",
        "length": 130,
        "severity": "ERROR",
        "code": "42703",
        "position": "704",
        "file": "parse_relation.c",
        "line": "3293",
        "routine": "errorMissingColumn",
        "sql": "SELECT \"Manufacturer\".\"id\", \"Manufacturer\".\"manufacturer_name\", \"Manufacturer\".\"manufacturer_logo_url\", \"Manufacturer\".\"manufacturer_archived_status\", \"Manufacturer\".\"createdAt\", \"Manufacturer\".\"updatedAt\", \"ManufacturerTabs\".\"id\" AS \"ManufacturerTabs.id\", \"ManufacturerTabs\".\"sequence\" AS \"ManufacturerTabs.sequence\", \"ManufacturerTabs\".\"tab_name\" AS \"ManufacturerTabs.tab_name\", \"ManufacturerTabs\".\"createdAt\" AS \"ManufacturerTabs.createdAt\", \"ManufacturerTabs\".\"updatedAt\" AS \"ManufacturerTabs.updatedAt\", \"ManufacturerTabs\".\"ManufacturerId\" AS \"ManufacturerTabs.ManufacturerId\" FROM \"Manufacturers\" AS \"Manufacturer\" LEFT OUTER JOIN \"ManufacturerTabs\" AS \"ManufacturerTabs\" ON \"Manufacturer\".\"id\" = \"ManufacturerTabs\".\"ManufacturerId\";"
    },
    "sql": "SELECT \"Manufacturer\".\"id\", \"Manufacturer\".\"manufacturer_name\", \"Manufacturer\".\"manufacturer_logo_url\", \"Manufacturer\".\"manufacturer_archived_status\", \"Manufacturer\".\"createdAt\", \"Manufacturer\".\"updatedAt\", \"ManufacturerTabs\".\"id\" AS \"ManufacturerTabs.id\", \"ManufacturerTabs\".\"sequence\" AS \"ManufacturerTabs.sequence\", \"ManufacturerTabs\".\"tab_name\" AS \"ManufacturerTabs.tab_name\", \"ManufacturerTabs\".\"createdAt\" AS \"ManufacturerTabs.createdAt\", \"ManufacturerTabs\".\"updatedAt\" AS \"ManufacturerTabs.updatedAt\", \"ManufacturerTabs\".\"ManufacturerId\" AS \"ManufacturerTabs.ManufacturerId\" FROM \"Manufacturers\" AS \"Manufacturer\" LEFT OUTER JOIN \"ManufacturerTabs\" AS \"ManufacturerTabs\" ON \"Manufacturer\".\"id\" = \"ManufacturerTabs\".\"ManufacturerId\";"
}

制造商型号:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Manufacturer = sequelize.define('Manufacturer', {
    manufacturer_name: DataTypes.STRING,
    manufacturer_logo_url: DataTypes.TEXT,
    manufacturer_archived_status: DataTypes.BOOLEAN
  }, {
    classMethods: {
      associate: function(models) {
        Manufacturer.hasMany(models.ManufacturerTab)
      }
    }
  });
  return Manufacturer;
};

ManufacturerTab型号:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var ManufacturerTab = sequelize.define('ManufacturerTab', {
    sequence: DataTypes.ARRAY(DataTypes.INTEGER),
    tab_name: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        ManufacturerTab.belongsTo(models.Manufacturer)
      }
    }
  });
  return ManufacturerTab;
};

制造商标签路线:

var models  = require('../models');
var express = require('express');
var router  = express.Router({mergeParams: true});

/* Get Manufacturer Tabs List */

router.get('/', function(req, res) {
  var manufacturer_id = req.params.manufacturer_id;
  models.Manufacturer.findAll({
    include: [models.ManufacturerTab]
  }).  
  then(function(manufacturers) {  
      res.status(200).json(manufacturers);  
  }, function(error) {  
      res.status(500).send(error);  
  });  
});

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cors = require('cors');

var routes = require('./routes/index');
var users  = require('./routes/users');
var manufacturers  = require('./routes/manufacturers');
var manufacturer_tabs  = require('./routes/manufacturer_tabs');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors({origin: 'http://localhost:4200'}));

app.use('/', routes);
app.use('/users', users);
app.use('/manufacturers', manufacturers);
app.use('/manufacturers/:manufacturer_id/manufacturer-tabs', manufacturer_tabs);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
// no stacktraces leaked to user unless in development environment
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: (app.get('env') === 'development') ? err : {}
  });
});


module.exports = app;

Config.js

module.exports = {
  development: {
    dialect: "postgres",
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOSTNAME
  },
  staging: {
    dialect: "postgres",
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOSTNAME
  },
  production: {
    dialect: 'postgres',
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOSTNAME
  }
};

3 个答案:

答案 0 :(得分:2)

当我们编写Manufacturer.hasMany(models.ManufacturerTab)并使用include查询时,续集将假定ManufacturerTab包含名为ManufacturerId的列。这充当可以进行联接操作的外键。

在您提供的模型定义中

module.exports = (sequelize, DataTypes) => {
  var ManufacturerTab = sequelize.define('ManufacturerTab', {
    sequence: DataTypes.ARRAY(DataTypes.INTEGER),
    tab_name: DataTypes.STRING
  }

有这样的列,因此也有错误。

如果您是使用迁移创建的数据库表,则需要定义一个新列

ManufacturerId: {
      type: Sequelize.INTEGER, // or choose what you want
      allowNull: false,
      references: {
        model: 'Manufacturer',
        key: 'id',
      },
      onDelete: 'CASCADE', // or choose what you want to do

答案 1 :(得分:0)

您的代码与创建connectionassociation没有任何关系。 将以下代码作为单独的文件包含在您拥有models

的目录中
fs.readdirSync(__dirname)
    .filter(function(file) {
        return (file.indexOf('.') !== 0) && (file !== basename) &&
            (file.slice(-3) === '.js');
    })
    .forEach(function(file) {
        var model = sequelize['import'](path.join(__dirname, file));
        db[model.name] = model;
    });

Object.keys(db).forEach(function(modelName) {
    if (db[modelName].associate) {
        db[modelName].associate(db);
    }
});

答案 2 :(得分:0)

您需要指定关联使用的外键,否则将使用默认值。将foreignKey 添加到belongsTo 和HasMany 关联中。

Manufacturer.hasMany(models.ManufacturerTab, {foreignKey: 'your-foreignKey'})
ManufacturerTab.belongsTo(models.Manufacturer, {foreignKey: 'your-foreignKey'})