如何在 knex 中将 hasColumn 与 where 子句一起使用

How can I use hasColumn with where clause in knex

有了knex,我有这个问题:

this.knex(this.table)
            .where(this.column_data, data)
            .where("archive", 1)

我想检查 this.table 是否有一个名为 'archive' 的列,所以我应该使用 'hasColumn' 方法

如何在前面的查询中使用schema.hasColumn(this.table,'archive')

我解决了这个问题:

this.knex.schema.hasColumn(this.table, "archive")
            .then(exists => {
                if (exists){
                    this.knex(this.table).where("archive", 1);
                }
                this.knex(this.table)
                    .where(this.column_data, data)
                    .then((datas)=> {
                    ........
                     });
            }).catch(err => {
                .....
            });