如何正确订购Sequelize中的选择?

时间:2016-07-11 10:32:04

标签: javascript postgresql sequelize.js

我无法获得http://docs.sequelizejs.com/en/latest/docs/querying/#ordering的非常简单的版本。请帮忙。

此查询:

const result = await db.dbo__peach_payments_transactions
.findOne({
  'order': [db.dbo__peach_payments_transactions, 'created_at', 'DESC'],
  'include': [{ model: db.dbo__orders, where: { 'users_id': userId} }]
});

给我错误: TypeError: Converting circular structure to JSON

此查询:

const result = await db.dbo__peach_payments_transactions.findOne({
  'order': 'dbo.peach_payments_transactions.created_at DESC',
  'include': [{ model: db.dbo__orders, where: { 'users_id': userId} }]
});

给我错误: "invalid reference to FROM-clause entry for table "peach_payments_transactions""

此查询看起来应该有效:

const result = await db.dbo__peach_payments_transactions.findOne({
  'order': '"dbo.peach_payments_transactions"."created_at" DESC',
  'include': [{ model: db.dbo__orders, where: { 'users_id': userId} }]
});

但是给了我错误: TypeError: Cannot set property 'order' of undefined

它输出了很好的SQL(参见最后一行):

SELECT 
  "dbo.peach_payments_transactions"."peach_payments_transactions_id", "dbo.peach_payments_transactions"."orders_id", "dbo.peach_payments_transactions"."payment_states_id", 
  "dbo.peach_payments_transactions"."response_data", "dbo.peach_payments_transactions"."token", "dbo.peach_payments_transactions"."result", "dbo.peach_payments_transactions"."reason_code", 
  "dbo.peach_payments_transactions"."reason_message", "dbo.peach_payments_transactions"."price", "dbo.peach_payments_transactions"."processed_at", "dbo.peach_payments_transactions"."created_at",
  "dbo.peach_payments_transactions"."updated_at", "dbo.order"."orders_id" AS "dbo.order.orders_id", "dbo.order"."users_id" AS "dbo.order.users_id", "dbo.order"."email" AS "dbo.order.email", 
  "dbo.order"."phone" AS "dbo.order.phone", "dbo.order"."token" AS "dbo.order.token", "dbo.order"."shipped_at" AS "dbo.order.shipped_at", "dbo.order"."created_at" AS "dbo.order.created_at", 
  "dbo.order"."updated_at" AS "dbo.order.updated_at" 
FROM 
  "dbo"."peach_payments_transactions" AS "dbo.peach_payments_transactions" 
  INNER JOIN "dbo"."orders" AS "dbo.order" 
  ON 
    "dbo.peach_payments_transactions"."orders_id" = "dbo.order"."orders_id" AND
    "dbo.order"."users_id" = '71e1989b-67d9-4712-ad71-51dfa56b32bd' 
  ORDER BY "dbo.peach_payments_transactions".created_at DESC LIMIT 1;

更新 - 模型

peach_payments_transactions:

    entity['peach_payments_transactions_id'] = {type: Sequelize.UUID, primaryKey: true, 'defaultValue': Sequelize.UUIDV4};
    entity['orders_id'] = helper.getForeignKey('orders_id', orders.getModel(db));
    entity['payment_states_id'] = helper.getForeignKey('payment_states_id', payment_states.getModel(db));
    entity['response_data'] = Sequelize.TEXT;
    entity['token'] = Sequelize.TEXT;
    entity['result'] = Sequelize.TEXT;
    entity['reason_code'] = Sequelize.TEXT;
    entity['reason_message'] = Sequelize.TEXT;
    entity['price'] = Sequelize.BIGINT;
    entity['processed_at'] = Sequelize.DATE;
    const result = db.define('dbo.peach_payments_transactions', entity, options);
    result.belongsTo(orders.getModel(db), {foreignKey: 'orders_id'});
    result.belongsTo(payment_states.getModel(db), {foreignKey: 'payment_states_id'});

订单

    entity['orders_id'] = {type: Sequelize.UUID, primaryKey: true, 'defaultValue': Sequelize.UUIDV4};
    entity['users_id'] = helper.getForeignKey('users_id', users.getModel(db));
    entity['email'] = Sequelize.TEXT;
    entity['phone'] = Sequelize.TEXT;
    entity['token'] = Sequelize.TEXT;
    entity['shipped_at'] = Sequelize.DATE;
    const result = db.define('dbo.orders', entity, options);
    result.belongsTo(users.getModel(db), {foreignKey: 'users_id'});

0 个答案:

没有答案