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 // Define the 'UserUploads' model const UserUploads = sequelize.define( 'UserUploads', { 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 uploads are also deleted }, file_name: { type: DataTypes.TEXT, allowNull: false, }, file_path: { type: DataTypes.TEXT, allowNull: false, }, file_type: { type: DataTypes.TEXT, allowNull: false, }, total_duration: { type: DataTypes.TEXT, allowNull: true, }, upload_day: { type: DataTypes.STRING(100), allowNull: false, }, upload_time: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, }, overall_metrics: { type: DataTypes.TEXT, allowNull: true, }, backhand: { type: DataTypes.TEXT, allowNull: true, }, forehand: { type: DataTypes.TEXT, allowNull: true, }, service: { type: DataTypes.TEXT, allowNull: true, }, smash: { type: DataTypes.TEXT, allowNull: true, }, }, { tableName: 'user_uploads', // Define the name of the table timestamps: false, // If you don't want Sequelize to automatically create `createdAt` and `updatedAt` columns } ); // User model associations User.hasMany(UserUploads, { foreignKey: 'user_id', onDelete: 'CASCADE', as: 'uploads', // Add alias 'uploads' here }); UserUploads.belongsTo(User, { foreignKey: 'user_id', as: 'user', // Optional alias for the reverse relation }); // Sync the model with the database const syncDb = async () => { try { await sequelize.sync({ force: false }); console.log('UserUploads table created (or already exists)'); } catch (err) { console.error('Error syncing the database:', err); } }; // Call the sync function syncDb(); export { UserUploads };