我有Users
表,并通过新的迁移文件向表中添加了两个新列email
和firstName
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
}
在我的日志中,我看到没有将值插入到新列email
和firstName
中,而旧列(如“用户名”等)没有问题。
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 *;
答案 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;
};