Mysql to Sequelize

Asked

Viewed 57 times

-1

I would like to turn this code into Mysql

SELECT pets.nome, pets.pulseira AS pets , localizacao.datahora, 
   localizacao.latitude, localizacao.longitude AS localizacao FROM pets 
   JOIN localizacao ON 
        localizacao.localPetID = id_pet where pets.id_pet  = 1

in a sequelize syntax.

So far all I’ve got is this:

pet.Pet.findAll({
        //selecionando os atributos nome e pulseira
        attributes: [
            'nome',
            'pulseira'
        ],
        //informando quais os valores para busca
        where: {
            nome: nomePet,
            pulseira: pulseira
        }
    }).then((pet) => {
        res.render('index', { pet: pet })
    })

they are returning me only the name and number of the bracelet, however, I need it to come together the data of datahora, latitude and longitude to display.

1 answer

1

To use the relationships, it is necessary to define them correctly in the model of each table. After creating the relationships you should pass the "include" property on the search object. Ex:

return pet.Pet.findAll({
            attributes: ['nome', 'pulseira'],
            include: [{
                model: sequelize.models.localizacao,
                as: 'localizacao',
                attributes: ['latitude', 'longitude'],
                required: true
            }],
            where: {
                nome: nomePet,
                pulseira: pulseira
            }
        }).then(pet => res.render('index', { pet }));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.