我是sails的初学者,我正在尝试使用sails和mysql创建我的第一个API。我尝试在我的mysql控制台中创建表,并将配置配置到configuration.js文件,并尝试在chrome浏览器中从POSTMAN访问API。我从sails服务器得到以下错误。我的查询没有该字段,也不确定这些字段是否为标准字段并在查询中附加,并且不确定如何自动将这些字段创建为表的一部分。
error: Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
: ER_BAD_FIELD_ERROR: Unknown column 'message.createdAt' in 'field list'
at Query.Sequence._packetToError (C:\MyApi\node_modules\sails-mysql\node_mod
ules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Query.ErrorPacket (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\l
ib\protocol\sequences\Query.js:82:18)
at Protocol._parsePacket (C:\MyApi\node_modules\sails-mysql\node_modules\mys
ql\lib\protocol\Protocol.js:271:23)
at Parser.write (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\lib\pr
otocol\Parser.js:77:12)
at Protocol.write (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\lib\
protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\
lib\Connection.js:82:28)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:765:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:427:10)
at emitReadable (_stream_readable.js:423:5)
at readableAddChunk (_stream_readable.js:166:9)
at Socket.Readable.push (_stream_readable.js:128:10)
at TCP.onread (net.js:529:21)
--------------------
at Protocol._enqueue (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\l
ib\protocol\Protocol.js:135:48)
at PoolConnection.Connection.query (C:\MyApi\node_modules\sails-mysql\node_m
odules\mysql\lib\Connection.js:185:25)
at __FIND__ (C:\MyApi\node_modules\sails-mysql\lib\adapter.js:836:20)
at afterwards (C:\MyApi\node_modules\sails-mysql\lib\connections\spawn.js:84
:5)
at C:\MyApi\node_modules\sails-mysql\lib\connections\spawn.js:40:7
at Ping.onPing [as _callback] (C:\MyApi\node_modules\sails-mysql\node_module
s\mysql\lib\Pool.js:94:5)
at Ping.Sequence.end (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\l
ib\protocol\sequences\Sequence.js:96:24)
at Ping.Sequence.OkPacket (C:\MyApi\node_modules\sails-mysql\node_modules\my
sql\lib\protocol\sequences\Sequence.js:105:8)
at Protocol._parsePacket (C:\MyApi\node_modules\sails-mysql\node_modules\mys
ql\lib\protocol\Protocol.js:271:23)
at Parser.write (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\lib\pr
otocol\Parser.js:77:12)
at Protocol.write (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\lib\
protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\MyApi\node_modules\sails-mysql\node_modules\mysql\
lib\Connection.js:82:28)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:765:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:427:10)
Details: Error: ER_BAD_FIELD_ERROR: Unknown column 'message.createdAt' in 'fiel
d list'
但是我收到了上述错误,下面是我的表格。
mysql> desc message
-> ;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| name | varchar(20) | YES | | NULL | |
| id | int(4) | NO | PRI | NULL | auto_increment |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.03 sec)
我在下面作为模型message.js中的代码
module.exports = {
connection: 'someMysqlServer',
attributes: {
name: { type: 'string',
required: true}
}
};
答案 0 :(得分:8)
似乎您可能在该连接上设置migrate: safe
,这意味着Sails无法更改您创建的MySQL表。这很好,除了Sails查询包括迁移器通常添加的createdAt
和updatedAt
字段。您可以手动将这些列添加到表中(使用datetime
类型),也可以将以下内容添加到模型配置中:
autoCreatedAt: false,
autoUpdatedAt: false
有关这些字段的更多信息in the docs。
答案 1 :(得分:1)