1
我正在創建一個簡單的「考勤」數據庫,在這個數據庫中,學生只能被允許每天「登記」一次。每個簽入將包含一個user_id,名稱,時間和日期。使用Knex,我怎麼只允許每個學生每天簽入一次。如何使用Knex將唯一的數據插入到Postgres中?
我試圖從this thread'whereNotExists'的幾個變體,但似乎沒有任何工作。
目前,這是我的插入語句:
db('checkins').insert(db
.select(attrs.user_id, attrs.user_name, attrs.date, attrs.created_at)
.whereNotExists(db('checkins').where('user_name', attrs.user_name).andWhere('date', attrs.date).then(() => {
console.log('successful insert')
}))
然後我收到此錯誤( 'alice_id' 是我 'attrs.user_id' 使用的測試值):
Unhandled rejection error: column "alice_id" does not exist
謝謝!這工作,但我最終改變使用複合索引,所以它不會失敗的任何競爭條件。像這樣: table.unique(['user_id','check_date']); –