首先,这是我的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来获取数据,同时为该Schema编写了相应的解析器(resolver)
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'
在 Sequelize 中,id
(以及 createdAt
、updateAt
)字段是自动生成的,不需要手动定义。
id
列。但这只能解释为什么有id
列,而不能解释为什么没有id
列。 - Tim Biegeleisen