【问题标题】:Field "SomeField" doesn't have a default value [closed]字段“SomeField”没有默认值[关闭]
【发布时间】:2019-08-20 11:47:02
【问题描述】:

我正在从 Postman 向 mySQL 数据库发送一些详细信息,它可以识别它,但我收到此错误:

code: 'ER_NO_DEFAULT_FOR_FIELD',
errno: 1364,
sqlMessage: 'Field \'creditcard\' doesn\'t have a default value',

我已将 mySQL 模式设置为传统并删除了严格模式,重新连接到数据库重新启动服务器,它再次抛出相同的错误。

以下是部分代码:

try {
  const createNewUser = 'INSERT INTO users (username, email, phonenumber, password, salt, created_at) VALUES (?,?,?,?,?,?)';
  con.query(createNewUser, [username, email, phonenumber, passHash, salt, createAt], (err, results) => {
    if (err) {
      console.error(err);
    }
    console.log(results);
  });
  res.status(201).send({ success: true, message: 'New user was successfully created', data: {username, email, phonenumber, password} });
} catch (error) {
  res.status(500).send({ success: false, message: 'Server error' });
}

await next;

}

const userCreateModel = `
  CREATE TABLE IF NOT EXISTS users (
    id INT(11) NOT NULL AUTO_INCREMENT,
    username VARCHAR(45) NOT NULL,
    email VARCHAR(45) NOT NULL,
    phonenumber VARCHAR(45) NOT NULL,
    password VARCHAR(255) NOT NULL,
    creditcard INT(11) NOT NULL,
    salt VARCHAR(255),
    created_at DATE,
    update_at DATE,
    deleted_at DATE,
    lastSignIn DATE,
    PRIMARY key (id)
  )
`;

提前谢谢你,如果有人知道我做错了什么,我会很高兴。

【问题讨论】:

  • creditcard INT(11) NOT NULL - 您是否提供了信用卡数据?
  • @tpschmidt 显然不是,因为该列没有出现在列列表中:)
  • 然后设置一个默认值creditcard INT(11) DEFAULT 0 NOT NULL,否则NOT NULL没有任何意义,对吧?
  • 即使没有 NOT NULL 也不起作用。
  • 你能把新建表的代码粘贴到这里吗

标签: mysql node.js express


【解决方案1】:
creditcard INT(11) NOT NULL,

在创建表时你提到它不应该接受空值并且在插入时你错过了提供信用卡信息

所以要么改变表以接受空值,要么在插入时提供信用卡值

【讨论】:

  • 我从信用卡中删除了 NOT NULL,但它仍然无法正常工作。
  • 因为 create table 有 create table IF NOT EXISTS 它可能没有创建新表。尝试显示创建表用户或更改现有表或删除旧表并创建一个新表
  • 我明白了!感谢您的帮助,这意味着很多!
【解决方案2】:

在您的尝试块中,您没有为您的信用卡声明一个字段

【讨论】:

    【解决方案3】:

    您的信用卡字段被定义为 Not Null,这就是当您没有为该字段提供任何值时出现此错误的原因。

    【讨论】:

    • 没有错误是因为在插入命令中没有为定义为 NOT NULL 的字段赋值
    • 是的,很抱歉我无法表达我的意思。编辑了我的答案,谢谢您的更正。
    猜你喜欢
    • 2012-09-25
    • 2018-03-19
    • 2014-11-09
    • 2015-07-07
    • 2019-11-22
    • 2018-07-29
    • 2018-07-09
    • 2018-07-14
    • 2020-05-29
    相关资源
    最近更新 更多