loopbackio / loopback-connector-mssql

LoopBack connector for Microsoft SQL Server
http://loopback.io/doc/en/lb3/SQL-Server-connector.html
Other
52 stars 84 forks source link

Loopback 4 relations compatibility #194

Closed lolocr closed 5 years ago

lolocr commented 5 years ago

I'm using loopback-connector-mssql with Loppback 4 version. When I have needed to use the relations with my models (belongsTo or Hasmany), I have the next error:

Unhandled error in GET /transactions-articles: 500 RequestError: Invalid column name 'RtvTransArticles'.
    at handleError (/Library/WebServer/Documents/rtv-node/node_modules/mssql/lib/tedious.js:546:15)
    at Connection.emit (events.js:182:13)
    at Parser.<anonymous> (/Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/connection.js:611:16)
    at Parser.emit (events.js:182:13)
    at Parser.<anonymous> (/Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/token/token-stream-parser.js:54:15)
    at Parser.emit (events.js:182:13)
    at addChunk (/Library/WebServer/Documents/rtv-node/node_modules/readable-stream/lib/_stream_readable.js:291:12)
    at readableAddChunk (/Library/WebServer/Documents/rtv-node/node_modules/readable-stream/lib/_stream_readable.js:278:11)
    at Parser.Readable.push (/Library/WebServer/Documents/rtv-node/node_modules/readable-stream/lib/_stream_readable.js:245:10)
    at Parser.Transform.push (/Library/WebServer/Documents/rtv-node/node_modules/readable-stream/lib/_stream_transform.js:148:32)
    at doneParsing (/Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/token/stream-parser.js:110:18)
    at /Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/token/infoerror-token-parser.js:46:5
    at /Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/token/infoerror-token-parser.js:13:19
    at /Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/token/stream-parser.js:247:9
    at Parser.awaitData (/Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/token/stream-parser.js:144:9)
    at Parser.readUInt32LE (/Library/WebServer/Documents/rtv-node/node_modules/tedious/lib/token/stream-parser.js:244:12)

Exists any restriction to use the Loopback 4 relations with the mssql connector? Thank you in advance.

dhmlau commented 5 years ago

@lolocr, there is no restriction of using LB4 relations with MSSQL connector. Do you have a sample app that can reproduce the problem? Thanks.

lolocr commented 5 years ago

Yes! I have a repo here: https://github.com/lolocr/lb4auth I'm trying to create a 'hasMany' relation between 'RtvTransactions' and 'RtvTransArticles', where 'RtvTransactions' hasMany 'RtvTransArticles'. I have defined the relation in the model and the injections in the repository (TransactionRepository), but when I call the '/transactions' endpoint the console shows me the previous error. Thanks.

dhmlau commented 5 years ago

@lolocr , sorry about the late reply. Thanks for your sample repo. Since I don't have the right database schema, so I've tried to reproduce using in-memory database and it seems to be working fine. Before going into further, may I ask a trivial question: The error says Invalid column name 'RtvTransArticles'. Do you actually have this column name in the specified table?

lolocr commented 5 years ago

@dhmlau, thank you for reply. I don't have any column with this name. I need it? I have tried to use the the name of the column (transaction_id) in the relation, but the problem continues.

dhmlau commented 5 years ago

@lolocr , sorry about the late response. If you use the column name that actually exists, do you get another set of errors?

lolocr commented 5 years ago

@dhmlau, finally I've been able to configure the relation between two models without errors. Now, when I try to include the relation in a query using the filter object, the framework shows this error: Relation "operatorGroup" is not defined in Operadores model. I've found that the error is in loopback-datasource-juggler/lib/include.js Can I use the 'include' filter value in my queries?

dhmlau commented 5 years ago

@lolocr, include filter is not currently supported. Please see our spike in https://github.com/strongloop/loopback-next/pull/2124, and the original issue: https://github.com/strongloop/loopback-next/issues/1352.

If you agree, I'd like to continue the discussion in https://github.com/strongloop/loopback-next/issues/1352. Thanks.

lolocr commented 5 years ago

Oks @dhmlau. Thanks.

JuliaRakitina commented 5 years ago

I spent half of the day to find it out( it does not work.

lolocr commented 5 years ago

Hello @JuliaRakitina, What's your issue? I recommend you to update the loopback libraries to the latest version because solve some issues (hasMany relations for example). In my case, the problem was in the definition of the relation. I needed to declare de 'keyTo' value. In my case I had a User model hasMany UserVisibility (in this model had the property loginId, that is related to 'id' of the User model): @hasMany(() => UserVisibility, {keyTo: 'loginId'}) userVisibilities?: UserVisibility[]; You can find more information here: https://github.com/strongloop/loopback-next/issues/1909 I hope it will help.

JuliaRakitina commented 5 years ago

@lolocr no, I need filter include

currently using work around

AnanthGopal commented 5 years ago

I try to get data like below (Parent and child data).

[{ "_id" : 1, "name" : "testTeam", "todos" : [{ "id" : "1", "username" : "test user" }] }, { "_id" : 2, "name" : "testTeam", "todos" : [ { "id" : "2", "username" : "test user1" }] }]

But I Got the error "the relation is not defined for model loopback 4" when I use include filter option. Below code, I used in my application

@get('/todo-lists', { responses: { '200': { description: 'Array of TodoList model instances', content: { 'application/json': { schema: { 'x-ts-type': TodoList } } }, }, }, }) async find( ): Promise<TodoList[]> { return await this.todoListRepository.find({ include: [{ relation: 'todos' }] }); }

dhmlau commented 5 years ago

@AnanthGopal, we're currently on adding support for inclusion of related models. Please see this epic: https://github.com/strongloop/loopback-next/issues/1352. Thanks.

AnanthGopal commented 5 years ago

Hi @JuliaRakitina

You mentioned, "currently using workaround", Can you explain about that, I got the same issue. Now I am using the Stored procedure to resolve that issue.

JuliaRakitina commented 5 years ago

Hi @AnanthGopal

Actually same :) I am using direct SQL query