Mohamed Ben HEnda
Mohamed Ben HEnda

Reputation: 2776

How can I use hasColumn with where clause in knex

With knex,I have this query:

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

I want to check if this.table has a column named 'archive', so I should use the 'hasColumn' method

How use schema.hasColumn(this.table, 'archive') in the previous query

Upvotes: 2

Views: 4421

Answers (1)

Mohamed Ben HEnda
Mohamed Ben HEnda

Reputation: 2776

I resolved this:

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 => {
                .....
            });

Upvotes: 4

Related Questions