书架和Knex SQLITE_ERROR:在“(”附近:

时间:2018-06-28 02:06:11

标签: javascript sqlite knex.js bookshelf.js

这是我的控制台及其记录的错误。我正在尝试编写一个脚本(createDB.js),以使用清单填充数据库。我很困惑为什么我得到一个SQLITE_ERROR。

$ backend node createDB.js
listing:  ModelBase {
  attributes:
   { icon: 'pin',
     title: 'Hatsushiba',
     category: 'station',
     lat: 1.213,
     lng: 1.452 },
  _previousAttributes: {},
  changed:
   { icon: 'pin',
     title: 'Hatsushiba',
     category: 'station',
     lat: 1.213,
     lng: 1.452 },
  relations: {},
  cid: 'c1' }
error Error: insert into  (`category`, `created_at`, `icon`, `lat`, `lng`, `title`, `updated_at`) values ('station', '2018-06-28 10:52:27.520', 'pin', 1.213, 1.452, 'Hatsushiba', '2018-06-28 10:52:27.520') - SQLITE_ERROR: near "(": syntax error

createDB.js

const Listing = require('./model')
const testListing = new Listing()
testListing.set('icon', 'pin')
testListing.set('title', 'Hatsushiba')
testListing.set('category', 'station')
testListing.set('lat', 1.213)
testListing.set('lng', 1.452)

testListing.save().then((listing) => {
  console.log(`saved ${listing.title} yo!`)
}).catch((err) => {
  console.log(`error ${err}`)
})

./ migrate / index.js

exports.up = function(knex) {
  return knex.schema
    .createTable('listings', function(table) {
      table.increments('id').primary()
      table.string('icon').defaultTo('pin')
      table.string('title', 128)
      table.string('category')
      table.float('lat')
      table.float('lng')
      table.timestamps()
    })
}

exports.down = function(knex) {
  return knex.schema
    .dropTable('listings')
}

model.js     const bookshelf = require('./ bookshelf')

const Listing = bookshelf.Model.extend({
  tablename: 'listings',
  hasTimestamps: true
})

module.exports = Listing

knexfile.js

module.exports = {
  client: 'sqlite3',
  connection: {
    filename: 'app.db'
  },
  useNullAsDefault: true
}

bookshelf.js

const path = require('path'),
      fs = require('fs'),
      knex = require('knex')(require('./knexfile'))

module.exports =  require('bookshelf')(knex)

1 个答案:

答案 0 :(得分:0)

在model.js中

const Listing = bookshelf.Model.extend({
  tablename: 'listings', <<====== change "tablename" to "tableName" =====<<
  hasTimestamps: true
})

module.exports = Listing