Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find or create with SequelizeJS

I try to use the "findOrCreate" function of SequelizeJS but it doesn't work.

The variable "created" is "undefined", so I don't know why because it's a variable used by SequelizeJS...

for( var i = 0; i < tags.length; i++ ){
    global.db.Tag.findOrCreate({name: tags[i]}).success( function(tag, created){
        if( created ){
            global.db.PostTag.create({
                PostId: id,
                TagId: tag.id
            });
        }
    });
}
like image 971
tonymx227 Avatar asked May 30 '13 23:05

tonymx227


People also ask

How do I use Find in Sequelize?

Finder methods are the ones that generate SELECT queries. By default, the results of all finder methods are instances of the model class (as opposed to being just plain JavaScript objects). This means that after the database returns the results, Sequelize automatically wraps everything in proper instance objects.

Does Sequelize automatically create table?

Automatically creates tables in MySQL database if they don't exist by calling await sequelize.

Does Sequelize auto generate ID?

Sequelize will assume your table has a id primary key property by default.

How do you create a Sequelized model?

Sequelize set up Install Sequelize database driver for the database you would like to use by running one of the commands below. Install npm package Sequelize-CLI. Create a project folder. In your project folder path, run the command below to initialize Sequelize in the folder.


1 Answers

I had the same problem. Upgrading from 1.7.0-alpha2 to 2.0.0-alpha2 solved it. Cheers

like image 137
Vincent Ghyssens Avatar answered Sep 26 '22 22:09

Vincent Ghyssens