【问题标题】:Bookshelf and Knex SQLITE_ERROR: near "(":书架和 Knex SQLITE_ERROR:靠近“(”:
【发布时间】:2018-06-28 02:06:11
【问题描述】:

这是我的控制台和它记录的错误。我正在尝试编写一个脚本(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)

【问题讨论】:

  • 其中一个问题是 SQLite 语句中没有包含表名……不知道为什么。
  • knex 有可用的种子命令。

标签: javascript sqlite knex.js bookshelf.js


【解决方案1】:

在model.js中

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

module.exports = Listing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    相关资源
    最近更新 更多