AI-Tennis-Coach/server/model/userAnswered.js
2025-02-11 11:23:59 +05:30

49 lines
1.4 KiB
JavaScript

import { sequelize } from './../loaders/index.js';
import { DataTypes } from 'sequelize';
import { User } from './user.js'; // Assuming your User model is in a file named user.model.js
const UserAnswers = sequelize.define('UserAnswers', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users', // The name of the referenced table (ensure it matches the table name)
key: 'id', // The primary key of the User model
},
onDelete: 'CASCADE', // If the user is deleted, their answers are also deleted
},
answers: {
type: DataTypes.TEXT,
allowNull: false,
}
}, {
tableName: 'user_answers', // Define the name of the table
timestamps: true, // If you don't want Sequelize to automatically create `createdAt` and `updatedAt` columns
});
UserAnswers.belongsTo(User, {
foreignKey: 'user_id',
as: 'user' // Optional alias for the reverse relation
});
// Sync the model with the database
const syncDbnow = async () => {
try {
await sequelize.sync({ force: false });
console.log('User Answer table created (or already exists)');
} catch (err) {
console.error('Error syncing the database:', err);
}
};
// Call the sync function
syncDbnow();
export { UserAnswers };