2017-03-14 2 views
0

bookshelf.js를 사용하고 있는데 데이터 형식 열을 변경하려고합니다.이 예가 제가 원하는 것입니다.Bookshelf 변경 열 데이터 형식

select * from table where date(datetime_col) = '2017-03-14' 

datatime_col은 DATETIME이고 쿼리를 실행하려면 날짜로 변환하고 싶습니다.

내가이 어떻게 할 수있는 전체 예제이 미안이

Error: ER_BAD_FIELD_ERROR: Unknown column 'date(datetime_col)' in 'where clause' 

위의 코드를 실행하려고 오류가

var Model = new model().query(function (qb) { 
     qb.where('date(datetime_col)', '=' , date); 
    }).fetchAll() 

책장에서 할 노력하고있어 어떻게 당신은 대답을 위해 감사를 위해 knex.raw와 bookshelf를 사용합니다. 사용 날짜 시간 열

var Model = new model().query(function (qb) { 
     qb.whereBetween(Bookshelf.knex.raw("DATE(colum_datetime)"), [date_var1, date_var2]); 
    }).fetchAll({ withRelated: ['table1', 'table2', {'table3':function (qb) { 
     qb.orderBy('date_column', 'desc') 
    }},'table4'] }); 

두 날짜 사이의 사용

그것은 그 "bookshelf.js"또는 기본 "knex"의 속성 이름을 얻을 것으로 예상처럼 보이는
var Model = new model().query("where", Bookshelf.knex.raw("DATE(colum_datetime)"), "=", date_var).fetchAll({ withRelated: ['table1', 'teble2', {'table3':function (qb) { 
    qb.orderBy('date_column', 'desc') 
}},'table4'] }); 

답변

1

모델 (예 : datetime_col)이고 date(datetime_col)과 같은 sql 조각은 아닙니다.

this source에 따르면 함수가있는 부분을 "원시"sql로 전달해야합니다. 제안 된 솔루션은 다음과 같이 작성하는 것입니다.

model.query("where", Bookshelf.knex.raw("DATE(field) = ?"), "2017-03-04")) 
+0

고맙게도 작동합니다. –