Reputation: 863
I am beginner to sails and I am trying to create my first API using sails and mysql. I tried to create the table in my mysql console and made the configuration to the configuration.js file and trying to access the API from POSTMAN in chrome browser. I am getting the below error from the sails server. My query doesn't have the field and not sure if these fields are standard and getting appended in the query and not sure how to create these as a part of table automatically.
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'
but I am getting the above error, below is my table.
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)
and I have the below as my code in model message.js
module.exports = {
connection: 'someMysqlServer',
attributes: {
name: { type: 'string',
required: true}
}
};
Upvotes: 5
Views: 3706
Reputation: 11
while liftng sails it will prompt for entering a number 1 for safe 2 for experimental like that..choose 2 it will work..worked for me..
Upvotes: 1
Reputation: 24958
It seems like you might have migrate: safe
set on that connection, which means that Sails isn't able to alter the MySQL table you've created. That's fine, except that Sails queries include the createdAt
and updatedAt
fields that the migrator typically adds. You can either add those columns to the table manually (with the datetime
type) or add the following to your model config:
autoCreatedAt: false,
autoUpdatedAt: false
More info about those fields in the docs.
Upvotes: 8