혼자 고민해보기_ 개발/TIL (Today I Learned)

20230705(수)_ 노드 JS 심화 강의 진행

nuri-story 2023. 7. 6. 10:26

금일 달성 항목

1)  노드 JS 심화 강의 수강


문제 해결 과정 1- UUID(범용 고유 식별자)

[문제]

user와 userinfo를 Transaction 하고 난 뒤 name을 수정을 하였는데  자꾸만 오류가 나는 상황이었습니다.

 

 

[시도 및 해결]

확인해보니 UUID(범용 고유 식별자)를 쓰고있으므로 Timestamp를 사용하지 않는다고 정의하지 않아서 생기는 문제였습니다.

UserHistories Model

'use strict';
const { Model } = require('sequelize');
const Sequelize = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class UserHistories extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here

      // 1. UserHistories 모델에서
      this.belongsTo(models.Users, { // 2. Users 모델에게 N:1 관계 설정을 합니다.
        targetKey: 'userId', // 3. Users 모델의 userId 컬럼을
        foreignKey: 'UserId', // 4. UserHistories 모델의 UserId 컬럼과 연결합니다.
      });
    }
  }

  UserHistories.init(
    {
      userHistoryId: {
        allowNull: false, // NOT NULL
        primaryKey: true, // Primary Key (기본키)
        type: Sequelize.UUID, // UUID의 최댓값으로 타입을 지정합니다.
        defaultValue: Sequelize.UUIDV4, // UUID를 기본 값으로 설정합니다.
      },
      UserId: {
        allowNull: false, // NOT NULL
        type: DataTypes.INTEGER,
        references: {
          model: 'Users', // Users 모델을 참조합니다.
          key: 'userId', // Users 모델의 userId를 참조합니다.
        },
        onDelete: 'CASCADE', // 만약 Users 모델의 userId가 삭제되면, Comments 모델의 데이터가 삭제됩니다.
      },
      beforeName: {
        allowNull: false, // NOT NULL
        type: DataTypes.STRING,
      },
      afterName: {
        allowNull: false, // NOT NULL
        type: DataTypes.STRING,
      }
    },
    {
      sequelize,
      modelName: 'UserHistories',
      timestamps: false, // createdAt, updatedAt 컬럼을 생성하지 않습니다.
    }
  );
  return UserHistories;
};

잘 실행이 되는 것을 확인할 수 있었습니다.

 

[알게된 점]

강의와 똑같지 않더라도 구글링해보고 공부해보면 금방 해결할 수 있다는 것을 알게되었습니다.