Resultado:
[{
"id":3,
"title":"go to the gym",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
},
{
"id":3,
"title":"stop the damn leafblowers",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
},
{
"id":3,
"title":"get the mail",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
},
{
"id":3,
"title":"get some headphones",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
}]
Comprobé el error en phpMyAdmin, ejecutando el código SQL:
SELECT * FROM `todos` INNER JOIN `users` ON `todos`.`user_id` = `users`.`id` WHERE `todos`.`user_id` = 3
Donde se muestran todas las columnas de las tablas.
Para corregir el problema, escogí establecer las columnas que debería devolver Knex, como en este SQL:
SELECT `todos`.`id`, `todos`.`title`, `todos`.`completed`, `todos`.`created_at`, `todos`.`updated_at`, `users`.`name`, `users`.`email` FROM `todos` INNER JOIN `users` ON `todos`.`user_id` = `users`.`id` WHERE `todos`.`user_id` = 3
Para eso se usa .column. Ciertamente más sencillo de construir con Knex que con SQL y el backtick.
El resultado es diferente:
[{
"id":3,
"title":"go to the gym",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
},
{
"id":4,
"title":"stop the damn leafblowers",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
},
{
"id":5,
"title":"get the mail",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
},
{
"id":6,
"title":"get some headphones",
"completed":0,
"created_at":"2018-05-10T03:33:42.000Z",
"updated_at":"2018-05-10T03:33:42.000Z",
"name":"Someone Else ",
"email":"test3@test.com"
}]
No hay comentarios:
Publicar un comentario