首先,这是关于MySQL的表格
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| MemberID | int(11) | NO | PRI | NULL | auto_increment |
| FirstName | varchar(50) | NO | | NULL | |
| LastName | varchar(50) | NO | | NULL | |
| CityID | int(11) | NO | MUL | NULL | |
+-----------+-------------+------+-----+---------+----------------+
我使用Sequelize创建了从Node JS服务器到MySQL的连接,如下所示:
var db = new Sequelize(
'member','root', null,
{
host : 'localhost',
port : 3306,
dialect : 'mysql'
}
);
然后我使用Sequelize在我的Node JS服务器上定义了MySQL表格,如下所示:
var memberlist = db.define('members', {
MemberID : Sequelize.INTEGER,
FirstName : Sequelize.STRING,
LastName : Sequelize.STRING
});
然后我创建了一个GraphQL Schema来获取数据以及模式的相应解析器
var schema = buildSchema(`
type Members {
MemberID: ID!
FirstName: String!
LastName: String!
CityID: City!
}
type City {
CityID: ID!
CityName: String!
}
type Query {
allMembers: [Members]!
}
`);
var root = {
allMembers : function(){
memberlist.find({}).complete(function (err,data)
{}).then(({data}) => data)},
}
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true
}));
当我尝试在GraphQL上运行查询时,出现以下错误:
Executing (default): SELECT `id`, `MemberID`, `FirstName`, `LastName`,
`createdAt`, `updatedAt` FROM `members` AS `members` LIMIT 1;
Unhandled rejection SequelizeDatabaseError: Unknown column 'id' in 'field
list'
id
(和createdAt
,updateAt
)字段来自哪里,因为我没有在Sequelize上定义它们?
答案 0 :(得分:0)
我在这里假设,如果你没有明确告诉sequelize主键在你的模型中是什么,它将假设它是一个名为id
的字段
尝试以下方法告诉你的主键是什么;
const memberlist = db.define('members', {
MemberID : {
type: DataTypes.INTEGER,
primaryKey: true,
}
FirstName : Sequelize.STRING,
LastName : Sequelize.STRING
});
为您自动添加createdAt和updatedAt字段。要停止发生这种情况,请使用
const memberlist = db.define('members', {
MemberID : {
type: DataTypes.INTEGER,
primaryKey: true,
}
FirstName : Sequelize.STRING,
LastName : Sequelize.STRING
}, {
timestamps: false
});