node.js 在“字段列表”中续写未知列“*.createdAt”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20386402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:26:11  来源:igfitidea点击:

Sequelize Unknown column '*.createdAt' in 'field list'

node.jssequelize.js

提问by David Limkys

I'm getting a Unknown column 'userDetails.createdAt' in 'field list' When trying to fetch with association.

我在“字段列表”中收到一个未知列“userDetails.createdAt”,当尝试通过关联获取时。

Using findAllwithout association works fine.

findAll无关联使用效果很好。

My code is as follows:

我的代码如下:

var userDetails = sequelize.define('userDetails', {
    userId :Sequelize.INTEGER,
    firstName : Sequelize.STRING,
    lastName : Sequelize.STRING,
    birthday : Sequelize.DATE
});

var user = sequelize.define('user', {
    email: Sequelize.STRING,
    password: Sequelize.STRING
});

user.hasOne(userDetails, {foreignKey: 'userId'});

user.findAll({include: [userDetails] }).success(function(user) {
    console.log(user)
});

回答by Jan Aagaard Meier

I think the error is that you have timestamps enabled in sequelize, but your actual table definitions in the DB do not contain a timestamp column.

我认为错误是您在 sequelize 中启用了时间戳,但是您在数据库中的实际表定义不包含时间戳列。

When you do user.find it will just do SELECT user.*, which only takes the columns you actually have. But when you join, each column of the joined table will be aliased, which creates the following query:

当您执行 user.find 时,它只会执行SELECT user.*,它只需要您实际拥有的列。但是当你加入时,加入的表的每一列都会被别名,这会创建以下查询:

SELECT `users`.*, `userDetails`.`userId` AS `userDetails.userId`,`userDetails`.`firstName` AS `userDetails.firstName`,`userDetails`.`lastName` AS `userDetails.lastName`, `userDetails`.`birthday` AS `userDetails.birthday`, `userDetails`.`id` AS `userDetails.id`, `userDetails`.`createdAt` AS `userDetails.createdAt`, `userDetails`.`updatedAt` AS `userDetails.updatedAt` FROM `users` LEFT OUTER JOIN `userDetails` AS `userDetails` ON `users`.`id` = `userDetails`.`userId`;

The fix would be to disable timestamps for either the userDetails model:

修复方法是禁用 userDetails 模型的时间戳:

var userDetails = sequelize.define('userDetails', {
    userId :Sequelize.INTEGER,
    firstName : Sequelize.STRING,
    lastName : Sequelize.STRING,
    birthday : Sequelize.DATE
}, {
    timestamps: false
});

or for all models:

或对于所有型号:

var sequelize = new Sequelize('sequelize_test', 'root', null, {
    host: "127.0.0.1",
    dialect: 'mysql',
    define: {
        timestamps: false
    }
});

回答by Pramod

I got the same error when migrating our project from laravel to featherjs. Tables are having column names created_at, updated_at instead of createdat, updatedat. I had to use field name mapping in Sequelize models as given below

将我们的项目从 laravel 迁移到 Featherjs 时,我遇到了同样的错误。表具有列名 created_at、updated_at 而不是 createdat、updatedat。我必须在 Sequelize 模型中使用字段名称映射,如下所示

  const users = sequelize.define('users', {
     id: {
         type: Sequelize.INTEGER,
         primaryKey: true
     },
     createdAt: {
         field: 'created_at',
         type: Sequelize.DATE,
     },
     updatedAt: {
         field: 'updated_at',
         type: Sequelize.DATE,
     },
     ..
     ..
     ..
     ..

回答by zhuxy

I got same error,two solutions:

我也遇到同样的错误,两种解决方法:

  1. when create table, add created_at and updated_at column.
  1. 创建表时,添加 created_at 和 updated_at 列。

CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', `name` varchar(30) DEFAULT NULL COMMENT 'user name', `created_at` datetime DEFAULT NULL COMMENT 'created time', `updated_at` datetime DEFAULT NULL COMMENT 'updated time', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='user';

CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', `name` varchar(30) DEFAULT NULL COMMENT 'user name', `created_at` datetime DEFAULT NULL COMMENT 'created time', `updated_at` datetime DEFAULT NULL COMMENT 'updated time', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='user';

  1. disable timestamps
  1. 禁用时间戳

const Proje

常量项目

ct = sequelize.define('project', {
    title: Sequelize.STRING,
    description: Sequelize.TEXT
},{
    timestamps: false
})

回答by MAITRAIYA MALI

Disable timestamps Ex:-

禁用时间戳例如:-

const User = sequelize.define('user', {
    firstName : Sequelize.STRING,
    lastName : Sequelize.STRING,
}, {
    timestamps: false
});

回答by Mark Sparrow

well, maybe too late but you can create createdAt, updatedAt when migration like

好吧,也许为时已晚,但您可以在迁移时创建 createdAt、updatedAt 之类的

      createdAt: {
        allowNull: false,
        defaultValue: Sequelize.fn('now'),
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        defaultValue: Sequelize.fn('now'),
        type: Sequelize.DATE
      }

Im using express and sequelize mysql then the model just define like normal for ex:

我使用 express 和 sequelize mysql 然后模型就像正常的 ex 一样定义:

module.exports = (sequelize, DataTypes) => {
  const Customer = sequelize.define('customer', {
    name: DataTypes.STRING,
    email: DataTypes.STRING,
    phone: DataTypes.TEXT,
    consider: DataTypes.TEXT,
    otherQuestion: DataTypes.TEXT
  }, {})
  return Customer