programing

Sequelize로 MariaDB에 값 'null'을 삽입하는 방법?

yellowcard 2023. 10. 17. 20:10
반응형

Sequelize로 MariaDB에 값 'null'을 삽입하는 방법?

코딩 학교의 데이터베이스를 관리하기 위해 노드.js에서 앱을 만들고 있습니다.지금까지 저는 코스와 선생님 두가지 모델을 가지고 있습니다.과정을 추가하기 위한 양식에 과정에 교사를 선택하지 않는 옵션을 삽입했습니다.

DB GUI를 사용할 때 'null' 값을 문제없이 삽입할 수 있습니다.

INSERT INTO courses (name, start_date, end_date, hours, teacher_id) VALUES ("Databases", '2020-04-18', '2020-04-19', 1, null);

하지만 형식상으로는 작동하지 않습니다.이 과정 모듈은 다음과(와 같습니다.

const { Sequelize, DataTypes } = require('sequelize');
const db = require('../config/database');
const Teachers = require('./Teachers');

const Courses = db.define('Courses', {
  course_id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING(40)
  },
  hours: {
    type: DataTypes.INTEGER
  },
  start_date: {
    type: DataTypes.DATEONLY
  },
  end_date: {
    type: DataTypes.DATEONLY
  },
  teacher_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    references: {
      model: Teachers,
      key: 'teacher_id'
    }
  }
}, {
  freezeTableName: true
});

Courses.belongsTo(Teachers, {foreignKey: 'teacher_id'});

module.exports = Courses;

과정을 추가하기 위해 양식에서 선택할 수 있는 옵션입니다.
(저도 시도해 보았습니다.value="NULL"(또는 빈 문자열).

<select 
  type="number"
  name="teacher_id" 
  id="teacher_id" 
  class="input-box">
    <option value="" selected disabled hidden>Please select</option>
    <option value="null">not determined yet</option>
    <option value="2">Jane Doe</option>
    <option value="5">Steven Smith</option>
    <option value="11">Arthur Miller</option>
</select>

이것은 나의 긴 오류 메시지입니다.


Executing (default): INSERT INTO `Courses` (`course_id`,`name`,`hours`,`start_date`,`end_date`,`teacher_id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?);
DatabaseError [SequelizeDatabaseError]: (conn=2067, no: 1366, SQLState: 22007) Incorrect integer value: 'null' for column `codingschool`.`courses`.`teacher_id` at row 1
sql: INSERT INTO `Courses` (`course_id`,`name`,`hours`,`start_date`,`end_date`,`teacher_id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?); - parameters:['HTML & CSS','1','2021-03-16','2021-03-19','null','2021-03-16 20:13:35.832','2021-03-16 20:13...]
    at Query.formatError (/Users/cb/Clients/Hamburg Coding School/node_modules/sequelize/lib/dialects/mariadb/query.js:280:16)
    at Query.run (/Users/cb/Clients/Hamburg Coding School/node_modules/sequelize/lib/dialects/mariadb/query.js:66:18)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async /Users/cb/Clients/Hamburg Coding School/node_modules/sequelize/lib/sequelize.js:619:16
    at async MySQLQueryInterface.insert (/Users/cb/Clients/Hamburg Coding School/node_modules/sequelize/lib/dialects/abstract/query-interface.js:748:21)
    at async model.save (/Users/cb/Clients/Hamburg Coding School/node_modules/sequelize/lib/model.js:3954:35)
    at async Function.create (/Users/cb/Clients/Hamburg Coding School/node_modules/sequelize/lib/model.js:2207:12) {
  parent: SqlError: (conn=2067, no: 1366, SQLState: 22007) Incorrect integer value: 'null' for column `codingschool`.`courses`.`teacher_id` at row 1
  sql: INSERT INTO `Courses` (`course_id`,`name`,`hours`,`start_date`,`end_date`,`teacher_id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?); - parameters:['HTML & CSS','1','2021-03-16','2021-03-19','null','2021-03-16 20:13:35.832','2021-03-16 20:13...]
      at Object.module.exports.createError (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/misc/errors.js:55:10)
      at PacketNodeEncoded.readError (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/io/packet.js:505:19)
      at Query.readResponsePacket (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/cmd/resultset.js:46:28)
      at PacketInputStream.receivePacketBasic (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/io/packet-input-stream.js:104:9)
      at PacketInputStream.onData (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/io/packet-input-stream.js:169:20)
      at Socket.emit (events.js:315:20)
      at addChunk (_stream_readable.js:309:12)
      at readableAddChunk (_stream_readable.js:284:9)
      at Socket.Readable.push (_stream_readable.js:223:10)
      at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
    fatal: false,
    errno: 1366,
    sqlState: '22007',
    code: 'ER_TRUNCATED_WRONG_VALUE_FOR_FIELD',
    sql: 'INSERT INTO `Courses` (`course_id`,`name`,`hours`,`start_date`,`end_date`,`teacher_id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?);',
    parameters: [
      'HTML & CSS',
      '1',
      '2021-03-16',
      '2021-03-19',
      'null',
      '2021-03-16 20:13:35.832',
      '2021-03-16 20:13:35.832'
    ]
  },
  original: SqlError: (conn=2067, no: 1366, SQLState: 22007) Incorrect integer value: 'null' for column `codingschool`.`courses`.`teacher_id` at row 1
  sql: INSERT INTO `Courses` (`course_id`,`name`,`hours`,`start_date`,`end_date`,`teacher_id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?); - parameters:['HTML & CSS','1','2021-03-16','2021-03-19','null','2021-03-16 20:13:35.832','2021-03-16 20:13...]
      at Object.module.exports.createError (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/misc/errors.js:55:10)
      at PacketNodeEncoded.readError (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/io/packet.js:505:19)
      at Query.readResponsePacket (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/cmd/resultset.js:46:28)
      at PacketInputStream.receivePacketBasic (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/io/packet-input-stream.js:104:9)
      at PacketInputStream.onData (/Users/cb/Clients/Hamburg Coding School/node_modules/mariadb/lib/io/packet-input-stream.js:169:20)
      at Socket.emit (events.js:315:20)
      at addChunk (_stream_readable.js:309:12)
      at readableAddChunk (_stream_readable.js:284:9)
      at Socket.Readable.push (_stream_readable.js:223:10)
      at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
    fatal: false,
    errno: 1366,
    sqlState: '22007',
    code: 'ER_TRUNCATED_WRONG_VALUE_FOR_FIELD',
    sql: 'INSERT INTO `Courses` (`course_id`,`name`,`hours`,`start_date`,`end_date`,`teacher_id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?);',
    parameters: [
      'HTML & CSS',
      '1',
      '2021-03-16',
      '2021-03-19',
      'null',
      '2021-03-16 20:13:35.832',
      '2021-03-16 20:13:35.832'
    ]
  },
  sql: 'INSERT INTO `Courses` (`course_id`,`name`,`hours`,`start_date`,`end_date`,`teacher_id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?);',
  parameters: [
    'HTML & CSS',
    '1',
    '2021-03-16',
    '2021-03-19',
    'null',
    '2021-03-16 20:13:35.832',
    '2021-03-16 20:13:35.832'
  ]
}

봐주셔서 정말 감사합니다!

null != null 문제로 보입니다.아마도 "null" 문자열을 삽입하려고 할 것입니다.따라서 오류 메시지가 나타납니다.tearcher_id정수 또는 null이어야 하지만 "null"은(는) 아닙니다.

과정 객체가 작성된 코드의 위치를 확인합니다.양식에서 "null" 옵션을 선택한 경우, 다음을 설정해야 합니다.course.teacher = null양식의 가치만 가져가지 않습니다.

언급URL : https://stackoverflow.com/questions/66662842/how-to-insert-value-null-into-mariadb-with-sequelize

반응형