J
J
jenya77712019-07-23 18:55:48
PostgreSQL
jenya7771, 2019-07-23 18:55:48

Sequelize PG query not working?

Hello, tell me why the request in Sequelize does not work, there is such an error in the response

SequelizeDatabaseError: missing FROM-clause entry for table "accounts"

'SELECT "proxy".*, "proxyAccounts"."id" AS "proxyAccounts.id" FROM (SELECT "proxy"."id", count("accounts"."id") AS "likecount" FROM "proxies" AS "proxy" LIMIT 1) AS "proxy" LEFT OUTER JOIN "accounts" AS "proxyAccounts" ON "proxy"."id" = "proxyAccounts"."id_proxy";'

const accounts = sequelize.define('accounts', {
    id_proxy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        foreignKey: true
    }
})

const proxy = sequelize.define('proxy', {
    take_at: {
        type: Sequelize.DATE,
    }
})

proxy.hasMany(accounts, { foreignKey: 'id_proxy', as: 'proxyAccounts' })
accounts.belongsTo(proxy, { foreignKey: 'id_proxy', as: 'proxyAccounts' })

models.proxy.findOne({
    attributes: ['id',
        [sequelize.fn('count', sequelize.col('accounts.id')), 'countConnect']
    ],
    where: {
        countConnect: {
            [Op.lte]: 10
        }
    },
    include: [{
        attributes: ['id'],
        model: models.accounts,
        as: 'proxyAccounts'
    }]
}).then(data => {

    console.log(data.dataValues)
}).catch(error => {

    console.error(error)
})

Through PgAdmin I looked at the names of the tables, and they are like this accounts, proxies.
Can you tell me how to make such a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alastor, 2019-07-23
@Alastor

You need to know:
1. What is the goal
2. An example of data in tables
3. An example of the result
But already at this stage I see that the joins are somehow used obliquely.
change accounts.id to proxyAccounts.id, no?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question