HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/good-life-be/models/newSubQuestions.js
import { Sequelize, DataTypes } from 'sequelize';
import sequelize from '../config/sequelize-config.js';

const NewSubQuestion = sequelize.define(
  'new_sub_questions',
  {
    id: {
      primaryKey: true,
      type: DataTypes.INTEGER,
      autoIncrement: true,
    },
    question_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    question: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    answer_type: {
      type: DataTypes.ENUM(
        'text',
        'dropdown',
        'radio',
        'checkbox',
        'time_range',
        'slider',
        'time',
        'duration',
        'date',
        'date_range',
        'button',
        'drag_drop',
        'text_duration',
        'multi_dropdown',
        'options'
      ),
      allowNull: false,
    },
    answers: {
      type: DataTypes.JSONB,
      allowNull: true,
    },
    parent_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    condition: {
      type: DataTypes.STRING(225),
      allowNull: true,
    },
    order: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      onUpdate: Sequelize.literal('CURRENT_TIMESTAMP'),
    },
  },
  {
    timestamps: true,
  }
);

NewSubQuestion.associate = (models) => {
  // Associating with the `newquestions` table
  NewSubQuestion.belongsTo(models.newquestions, {
    foreignKey: 'question_id',
    as: 'newquestions',
  });

  // Associating with itself for parent-child relationship
  NewSubQuestion.belongsTo(models.new_sub_questions, {
    as: 'parentQuestion',
    foreignKey: 'parent_id',
  });
  NewSubQuestion.hasMany(models.new_sub_questions, {
    as: 'childQuestions',
    foreignKey: 'parent_id',
  });
};

export default NewSubQuestion;