홈>
나는 api 경로가 sequelize에 중첩 된 열망하는로드가있는 큰 객체를 반환하도록하려고합니다. 내 "레인"모델에는 "위치"모델과 'origin_location_id'및 'destination_location_id'라는 두 가지 연결이 있습니다. 'origin_location_id'및 'destination_location_id'레코드를 모두 반환하려고하지만 'destination_location_id'레코드 만 가져옵니다.
"as"와 "through"를 사용하여 쿼리에서 모델 정의의 연관, 모델 정의의 여러 변형을 역전 시키려고 시도했지만 서버가 아닌 응답을 얻지 못했습니다 ( 오류 코드 또는 오류 설명이 아닌) 오류가 발생하면 로딩 오류가 발생하거나 json 결과에 위치 레코드가 하나만 표시됩니다.
다음은 질의입니다 :
(function(req,res) {
models.Trip.findAll({
attributes: ['id', 'createdAt'],
include: [
{ model: models.Customer, attributes: ['id', 'name', 'email', 'address', 'phone'] },
{ model: models.Move, attributes: ['id', 'trip_id'], include: [
{ model: models.Lane, attributes: ['origin_location_id', 'destination_location_id', 'duration', 'distance'], include: [
{ model: models.Location, attributes: ['id', 'tookan_id', 'name', 'address', 'email', 'phone'] }
] }
] }
],
order:[['createdAt', 'DESC']]}).then(trips => {return res.json(trips)}).catch(err => res.status(422).json(err))})
레인 모델 정의는 다음과 같습니다.
'use strict';
module.exports = (sequelize, DataTypes) => {
const Lane = sequelize.define('Lane', {
active: {
type: DataTypes.TINYINT,
defaultValue: true },
customer_id: DataTypes.INTEGER,
description: DataTypes.TEXT,
origin_location_id: DataTypes.INTEGER,
destination_location_id: DataTypes.INTEGER,
distance: DataTypes.INTEGER,
duration: DataTypes.INTEGER,
driver_base_pay: DataTypes.DECIMAL,
driver_return_pay: DataTypes.DECIMAL,
tolls: DataTypes.DECIMAL,
driver_pay_per_minute: DataTypes.DECIMAL,
driver_pay_per_kilometer: DataTypes.DECIMAL,
average_drive_speed: DataTypes.DECIMAL
}, {});
Lane.associate = function(models) {
models.Lane.belongsTo(models.Customer, {foreignKey: 'customer_id'});
models.Lane.belongsTo(models.Location, {foreignKey: 'origin_location_id'});
models.Lane.belongsTo(models.Location, {foreignKey: 'destination_location_id'});
models.Lane.hasMany(models.Move, {foreignKey: 'lane_id'});
};
return Lane;
};
위치 모델 정의는 다음과 같습니다.
'use strict';
module.exports = (sequelize, DataTypes) => {
const Location = sequelize.define('Location', {
tookan_id : DataTypes.INTEGER,
active: {
type: DataTypes.TINYINT,
defaultValue: true },
customer_id: DataTypes.INTEGER,
name: DataTypes.TEXT,
address: DataTypes.TEXT,
email: DataTypes.TEXT,
phone: DataTypes.TEXT
}, {});
Location.associate = function(models) {
models.Location.belongsTo(models.Customer, {foreignKey: 'customer_id'});
models.Location.hasMany(models.Lane, {foreignKey: 'origin_location_id'});
models.Location.hasMany(models.Lane, {foreignKey: 'destination_location_id'});
};
return Location;
};
현재 JSON 결과 구조는 다음과 같습니다 (두 필드에서 Locations와 연관되어 있음에도 불구하고 각 Lane 레코드에 대해 하나의 Location 레코드 만 반환 됨). 와이즈 비즈
그리고 여기에 내가 얻는 오류가 있습니다 :
{
"id": 27,
"createdAt": "2018-09-20T12:30:32.000Z",
"Customer": {
"id": 1,
"name": "",
"email": "",
"address": "",
"phone": ""
},
"Moves": [{
"id": 29,
"trip_id": 27,
"Lane": {
"id": 4,
"origin_location_id": 3,
"destination_location_id": 1,
"duration": 1260,
"distance": 21082,
"driver_base_pay": "20",
"driver_return_pay": "3",
"driver_pay_per_kilometer": "1",
"average_drive_speed": "18",
"Location": {
"id": 1,
"tookan_id": null,
"name": "",
"address": "",
"email": "",
"phone": ""
}
}
},
{
"id": 26,
"trip_id": 27,
"Lane": {
"id": 3,
"origin_location_id": 1,
"destination_location_id": 3,
"duration": 1260,
"distance": 21082,
"driver_base_pay": "20",
"driver_return_pay": "3",
"driver_pay_per_kilometer": "1",
"average_drive_speed": "18",
"Location": {
"id": 3,
"tookan_id": null,
"name": "",
"address": "",
"email": "",
"phone": ""
}
}
}
]
}
내가 이것을 시도 할 때 :
{
"name": "SequelizeEagerLoadingError"
}
(function(req,res) {
models.Trip.findAll({
include: [
{ model: models.Customer },
{ model: models.Move, include: [
{ model: models.Lane, include: [
{ model: models.Location, as: 'origin_location_id' },
{ model: models.Location, as: 'destination_location_id'}
] }
] }
],
order:[['createdAt', 'DESC']]
}).then(trips => {return res.json(trips)})
.catch(err => res.status(422).json(err))
})
- 답변 # 1
관련 자료
- eloquent - Laravel에서 eager loading에서 중첩 데이터를 얻는 방법은 무엇입니까?
- .net - 쿼리에서 중첩 된 case 문으로 레코드를 업데이트하지 않는 SQL 서버
- node.js - Sequelize의 중첩 된 내부 조인
- Laravel의 세 번째 수준 관계에 대한 Lazy Eager Loading
- mysql - sequelize에서 중첩 된 select 문을 작성하는 방법은 무엇입니까?
- c# : Entity Framework에서 열렬한로드를 사용할 때 관련 데이터가 포함되어 있는지 어떻게 알 수 있습니까?
- angular7 router - 각도 7 중첩 지연 로딩 문제
- java - SpringBoot 중첩 된 속성으로 레코드를 찾는 방법
- javascript - 중첩 된 경로 Vue JS에서 이미지가로드되지 않음
- java : Spring Data JPA에서 열심히로드하는 연결된 컬렉션을 열망하는 모델 매퍼를 방지하는 방법은 무엇입니까?
- 하나의 SQL 쿼리에서 중첩 레코드 업데이트 (활성 레코드, 루비 온 레일)
- 관계 데이터로 웅변하는 라 라벨 (Eager Loading)
- php - Laravel Eager Loading, 여러 개의 동일한 hasOne 관계
- laravel - 페이지 매김으로 열망하는 로딩
- python - 필드가 중첩 된 객체로드/직렬화 해제
관련 질문
- node.js : Sequelize-cli를 사용하여 모델을 생성할 때 유효하지 않거나 예기치 않은 토큰
- node.js : sequelize로 uuidv4를 뿌리는 방법
- javascript - Sequelize Create 메서드를 사용할 때 특정 특성 만 반환하는 방법
- graphql - GraphQLList를 사용하여 여러 테이블에서 데이터를 가져올 수 있습니까?
- node.js - 시클 리즈 클리 모델 생성
- node.js - sequelize v6를 사용하여 ES6 구문에서 모델을 연결하는 방법
- javascript - 연관을 정의 할 때 sequelize get 오류
- mysql - 사용자에 ""별칭이있는 Sequelize Association이 없습니다.
- mysql : Sequelize include 옵션은 다른 모델을 가져올 수 없습니다.
- mysql : 프로젝트 데이터베이스에서 모델 생성 중 Sequelize CLI 오류
지난 한 시간 동안 해왔 던 것과 같은 일을했는데 연관에 "as"문을 추가하고 쿼리에 대응 시키려고하지만 갑자기 아무 이유없이 작동합니다. 한 시간 전에는 작동하지 않았습니다. 내가 한 것은 차선-위치 연결을 와이즈 비즈 그런 다음 일치하도록 쿼리를 업데이트했습니다. 와이즈 비즈
models.Lane.belongsTo(models.Location, { as: 'pickup', foreignKey: 'origin_location_id'}); models.Lane.belongsTo(models.Location, { as: 'delivery', foreignKey: 'destination_location_id'});