Synchronizing the model with the database
If you want Sequelize to automatically create the table (or modify it as needed) according to your model definition, you can use the sync
method, as follows:
// Note: using `force: true` will drop the table if it already exists
User.sync({ force: true }).then(() => {
// Now the `users` table in the database corresponds to the model definition
return User.create({
firstName: 'John',
lastName: 'Hancock'
});
});
Synchronizing all models at once
Instead of calling sync()
for every model, you can call sequelize.sync()
which will automatically sync all models.
Note for production
In production, you might want to consider using Migrations instead of calling sync()
in your code. Learn more in the Migrations guide.