Hi,
I am trying to connect to a mysql db and specify the table and column name. I have read this strongloop/loopback-next#2134 tried the below and still not working.
@model({
settings: {
mysql: {
table: 'area' // lowercase
}
}
})
export class Area extends Entity {
@property({
type: 'boolean',
mysql: {
columnName: 'category_id'
}
})
categoryId?: boolean;
constructor(data?: Partial<Area>) {
super(data);
}
}
ERROR
Unhandled error in GET /areas: 500 Error: ER_NO_SUCH_TABLE: Table 'mydb.Area' doesn't exist
at Query.Sequence._packetToError (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (D:\Lavoro\PassBot-LB4\node_modules\mysql\lib\Connection.js:525:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:597:20)
Just out of curiosity I set to lowercase all the query at \node_modules\loopback-connector-mysql\lib\mysql.js:197:16 with connection.query(sql.toLowerCase(), params, function (err, data) but this time the error is for the column name:
Unhandled error in GET /areas: 500 Error: ER_BAD_FIELD_ERROR: Unknown column 'categoryid' in 'field list'
Here is my db.datasource.ts:
{
"host": "myhost:port",
"port": 3306,
"url": "mysql://username:passsword@myhost:port/mydb",
"database": "mydb",
"password": "password",
"name": "db",
"user": "username",
"connector": "mysql",
"debug": "true",
"charset": "utf8mb4",
"collation": "utf8mb4_general_ci",
"maxDepthOfQuery": 20
}
@OrderConcept, it seems to be working for me for mysql.
I have a customer table with custid and name as columns.
@model({
settings: {
mysql: {
table: 'customer',
},
},
})
export class Abc extends Entity {
@property({
type: 'string',
id: true,
mysql: {
columnName: 'custid',
},
})
id?: string;
@property({
type: 'string',
mysql: {
columnName: 'name',
},
})
abcname?: string;
The only difference is that I'm using url in datasource, but it shouldn't make a difference:
{
"name": "ds",
"connector": "mysql",
"url": "mysql://root:pass@localhost/testdb",
"host": "",
"port": 0,
"user": "",
"password": "",
"database": ""
}
The error Table 'mydb.Area' doesn't exist looks suspicious. It means it's not looking up area which you set in the settings.
The only difference is that I'm using url in datasource, but it shouldn't make a difference:
I'm using url in datasource too, I had only omitted in the previous post (now update).
The error Table 'mydb.Area' doesn't exist looks suspicious. It means it's not looking up area which you set in the settings.
Exactly, that's why I try to hard lowercase the query and as expected it fails on column name (not on table name).
It seems that my custom settings (on table and column) are simply ignored by lb.
Can you try to run npm run clean to clean up the dist folder? (just in case)
I've put my sample repo here: https://github.com/dhmlau/test-0524/blob/master/src/models/abc.model.ts. But I don't see anything different that you're doing here.
Ya npm run clean solve the "problem", I don't know why it didn't occur to me before :(
Thank you @dhmlau
Most helpful comment
Can you try to run
npm run cleanto clean up the dist folder? (just in case)I've put my sample repo here: https://github.com/dhmlau/test-0524/blob/master/src/models/abc.model.ts. But I don't see anything different that you're doing here.