【问题标题】:Bookshelf JS Messing With Tablename书架 JS 与表名混淆
【发布时间】:2020-03-28 04:40:46
【问题描述】:

简介

我正在尝试使用 bookshelf.js 和 knex.js 库对 mssql 2019 中的表进行简单查询。该表是架构的一部分。

问题描述

我的 sqlserver 中出现 sql 错误,因为 bookshelf.js 或 knex.js 通过修改我的表名弄乱了我的架构: 我想知道是否有办法让我的表名保持原样或将我的架构定义为自己的字段。

重现问题的步骤

我有一个用 bookshelf.js 定义的用户模型,如下所示:

const User = bookshelf.model('User', {
    tableName: '[thanos.User]'
});

使用 bookshelf.js 的 count() 函数:

User.count().then((count) => {
    console.log('number of users:', count)
})

预期行为

sql: select count(*) as [count] from [thanos.User]

实际行为

sql: select count(*) as [count] from [thanos].[User]

【问题讨论】:

标签: sql node.js sql-server knex.js bookshelf.js


【解决方案1】:

我不知道框架,但根据您所看到的,我的猜测是您的模型定义中的 [] 可能会被删除。

他们是否需要在这个框架中转义 - 例如类似 `tablename: '[[thanos.user]' ?

【讨论】:

  • 我试过 '[[thanos.user]]' 和 '\[thanos.user\]',还有一个 'escape' 函数定义在 api 文档bookshelfjs.org/api.html#Model-instance-escape ,但它似乎不是我想要的。
  • github.com/bookshelf/bookshelf 建议它只支持 MySQL、PostgreSQL 和 SQLite3。我不太熟悉它们如何处理模式,但它可能与 SQL Server 不同,这可能是您问题的根源?
猜你喜欢
  • 2016-12-06
  • 2015-10-22
  • 2010-10-29
  • 2011-03-06
  • 1970-01-01
  • 2018-01-25
  • 2023-03-31
  • 1970-01-01
  • 2010-09-27
相关资源
最近更新 更多