Sequelize不会在新添加的列中插入值

时间:2018-12-04 09:44:45

标签: node.js sequelize.js graphql graphql-js sequelize-cli

我有Users表,并通过新的迁移文件向表中添加了两个新列emailfirstName

sequelize db:create --name add_columns_to_users

add_columns_to_users.js:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {

    return [
      queryInterface.addColumn(
        'Users',
        'email',
        {
          type: Sequelize.TEXT,
        }
      ),
      queryInterface.addColumn(
        'Users',
        'firstName',
        {
          type: Sequelize.TEXT,
        }
      )
    ];
  },

  down: (queryInterface, Sequelize) => {
  }
};

user / schema.graphql:

type User {
  id: ID!
  username: String
  email: String
  firstName: String
}

type Query {
  users(id: ID): [User]
}

input CreateUserInput {
  username: String!
  email: String!
  firstName: String!
}

type Mutation {
  createUser (input: CreateUserInput!): User
}

在我的用户更改中,我有以下内容:

Mutation: {
    createUser(root, {input}, context){
    models.User.create(input).then( user => {
        console.log(user);
    });
  }
}

这是我发送的输入:

{ 
  'username': profile.username,
  'email': profile.email,
  'firstName': profile.firstName
}

在我的日志中,我看到没有将值插入到新列emailfirstName中,而旧列(如“用户名”等)没有问题。

INSERT INTO "Users" ("id","username","createdAt","updatedAt") VALUES 
(DEFAULT,'Sxxxxh RxxxF','2018-12-04 08:34:11.974 +00:00','2018-12-04 08:34:11.974 +00:00') 
RETURNING *;

1 个答案:

答案 0 :(得分:0)

我真傻!我忘记将新列添加到 models / user.js

models / user.js:

'use strict';

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    username: DataTypes.TEXT,
    password: DataTypes.TEXT,
    token: DataTypes.TEXT,
    abbr: DataTypes.TEXT,
    deleted: DataTypes.BOOLEAN,
    email: DataTypes.TEXT,
    firstName: DataTypes.TEXT
  }, {});

  User.associate = function(models) {
  };
  return User;
};