-1
私は時間と無駄を失ったという質問があります。SequelizeとNodeJSをプライマリキーの別のキーで使用してクエリを作成
私は1つのモデルの価格を持っている:
var price = sequelize.define('price', {
price_id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataType.INTEGER
},
carrier_mongo_id : {
type: DataType.STRING,
require : true,
references: {
model: "carrier",
key: "carrier_mongo_id"
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
value: {
type: DataType.DECIMAL
}
});
私は他のモデルのキャリアがあります。
var carrier = sequelize.define('carrier', {
carrier_id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Datatype.INTEGER
},
carrier_mongo_id: {
type: Datatype.STRING,
require : true
},
name: {
type: Datatype.STRING,
require : true
}
});
と、次のようにクエリを行います。
carrierModel.hasMany(priceModel, {foreignKey: 'carrier_mongo_id'});
carrierModel.findAll({ include: [{ model: priceModel , required: true }] }).then(function(result){
var aCarriers = new Array();
result.forEach(function(carreteiro){
aCarriers.push(carreteiro.dataValues);
});
console.log(aCarriers);
}).catch(function(err){
console.log(err);
});
を私のクエリは次のとおりです。
SELECT
`carrier`.`carrier_id`,
`carrier`.`carrier_mongo_id`,
`carrier`.`name`,
`prices`.`price_id` AS `prices.price_id`,
`prices`.`carrier_mongo_id` AS `prices.carrier_mongo_id`,
`prices`.`value` AS `prices.value`
FROM
`carrier` AS `carrier`
INNER JOIN `price` AS `prices` ON `carrier`.`carrier_id` = `prices`.`carrier_mongo_id`
しかし、私は、このSQLクエリ必要があります:私は、このメソッドのSequelize.query(...)を使用することができることを知っているが、私の質問がある
SELECT
`carrier`.`carrier_id`,
`carrier`.`carrier_mongo_id`,
`carrier`.`name`,
`prices`.`price_id` AS `prices.price_id`,
`prices`.`carrier_mongo_id` AS `prices.carrier_mongo_id`,
`prices`.`value` AS `prices.value`
FROM
`carrier` AS `carrier`
INNER JOIN `price` AS `prices` ON `carrier`.`carrier_mongo_id` = `prices`.`carrier_mongo_id`
を:
Sequelizeを使って、このクエリを作成する方法モデル
TKS
ため
TKSは、なぜあなたは代わりに '' integer'のSTRING'を使用していますか? 'carrier_mongo_id'を' integer'として定義しようとします。 –
こんにちは@TilovYrys、carrier_mongo_idはハッシュIDのtksです:) – eHattori