I am trying to parse a csv file with fast-csv and at the end store all the lines in a database with sequelize
Here is my script to parse the data:
module.exports = async () => {
try {
let csvData = [];
csv
.parseFile(__basedir + "/test.csv", { headers: true })
.on('data', (data) => {
csvData.push(data);
})
.on('end', () => {
Card_Report.bulkCreate(csvData)
});
} catch (e) {
console.error('Something went wrong', e)
}
}
Here is my table model:
const Test= sequelize.define(
'test',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: uuidv4(),
allowNull: false
},
name: {
type: DataTypes.STRING,
},
age: {
type: DataTypes.INTEGER,
}
}, {
createdAt: false,
updatedAt: false,
tableName: 'test'
}
)
return Test
}
and here is my csv file :
name, age
nina, 24
sarah, 23
when I run the script I get this error :
Unhandled rejection SequelizeUniqueConstraintError: Validation error
But when there is only one row in my csv file it adds the row to the table correctly
How can I fix this?
It seems you are getting the same id, because all models in Sequelize are "prepared" once, and your function uuidv4()
runs only once. Thus for every row you insert into the DB it will have the same uuid. You could try mitigating it like this:
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4, <- try something like this
primaryKey: true,
},
Thus you are shifting responsibility for generating a unique UUID for each row on the database
P.S.: You might need a uuid extension for your DB, something like this:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";