Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: mysql数据库、json类型时,不能正常设置默认值 #382

Closed
shumyun opened this issue Sep 22, 2021 · 4 comments
Closed

Bug: mysql数据库、json类型时,不能正常设置默认值 #382

shumyun opened this issue Sep 22, 2021 · 4 comments

Comments

@shumyun
Copy link

shumyun commented Sep 22, 2021

Describe the bug

使用这个

Tables.extend('user', {
  fields: {
    records: { type: 'json', initial: { cur_task: null, surplus: {} } },
  },
});

还是这个

Tables.extend('user', {
  fields: {
    records: { type: 'json', initial: JSON.stringify({ cur_task: null, surplus: {} }) },
  },
});

都出现以下错误:

D:\personal\youkey-bot\node_modules\mysql\lib\protocol\Parser.js:437
      throw err; // Rethrow non-MySQL errors
      ^

SyntaxError: Unexpected end of JSON input
    at PoolConnection.typeCast (D:\personal\youkey-bot\node_modules\koishi-plugin-mysql\lib\index.js:143:28)
    at RowDataPacket.parse (D:\personal\youkey-bot\node_modules\mysql\lib\protocol\packets\RowDataPacket.js:33:24)
    at Query.RowDataPacket (D:\personal\youkey-bot\node_modules\mysql\lib\protocol\sequences\Query.js:153:10)
    at Protocol._parsePacket (D:\personal\youkey-bot\node_modules\mysql\lib\protocol\Protocol.js:260:14)
    at Parser._parsePacket (D:\personal\youkey-bot\node_modules\mysql\lib\protocol\Parser.js:433:10)
    at Parser.write (D:\personal\youkey-bot\node_modules\mysql\lib\protocol\Parser.js:43:10)
    at Protocol.write (D:\personal\youkey-bot\node_modules\mysql\lib\protocol\Protocol.js:38:16)
    at Socket.<anonymous> (D:\personal\youkey-bot\node_modules\mysql\lib\Connection.js:88:28)
    at Socket.<anonymous> (D:\personal\youkey-bot\node_modules\mysql\lib\Connection.js:526:10)

Steps to reproduce

矛头都指向这里

      typeCast: (field, next) => {
        const { orgName, orgTable } = field.packet
        const type = MysqlDatabase.tables[orgTable]?.[orgName]
        if (typeof type === 'object') return type.parse(field)

        const meta = Koishi.Tables.config[orgTable]?.fields[orgName]
        if (meta?.type === 'string') {
          return field.string()
        } else if (meta?.type === 'json') {
          return JSON.parse(field.string()) || meta.initial
        } else if (meta?.type === 'list') {
          const source = field.string()
          return source ? source.split(',') : []
        }

        if (field.type === 'BIT') {
          return Boolean(field.buffer()?.readUInt8(0))
        } else {
          return next()
        }
      },

Expected behavior

我改了以下代码:
return JSON.parse(field.string()) || meta.initial

return meta.initial || JSON.parse(field.string())

最后数据库都没有默认值

Screenshots

No response

Versions

  • OS:
  • Platform:
  • Node version:
  • Koishi version:

Additional context

No response

@shumyun shumyun added the bug BUG label Sep 22, 2021
@shumyun
Copy link
Author

shumyun commented Sep 22, 2021

发现mysql 对json类型不能设置默认值,但是系统没有做处理。会提示错误!

@shigma
Copy link
Member

shigma commented Sep 23, 2021

你能看下数据库中这个字符串的值是什么吗?是空串吗?

@shumyun
Copy link
Author

shumyun commented Sep 24, 2021

是空串。不是null

@shigma
Copy link
Member

shigma commented Dec 3, 2021

预计将在下个版本修复。

@shigma shigma added the commited label Dec 3, 2021
@shigma shigma closed this as completed Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants