3

I'm trying to properly query all images that fit my sequelize query in addition to the description that is connected to the specific query, but I receive an error for a createdAt column, which is not located in my table. How can I specify the columns I want to use within my query?

Here is the query (The pattern and color are correctly pulled into the query):

router.get('/:pattern/:color/result', function(req, res){

    console.log(req.params.color);
    console.log(req.params.pattern);

    Images.findAll({ 
        where: {
            pattern: req.params.pattern,
            color: req.params.color
        }
        });
        //console.log(image);
        //console.log(doc.descriptions_id);
        res.render('pages/result.hbs', {
            pattern : req.params.pattern,
            color : req.params.color,
            image : image
        });

});

Here is my table:

CREATE TABLE `images` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `pattern` varchar(225) DEFAULT NULL,
  `color` varchar(225) DEFAULT NULL,
  `imageUrl` varchar(225) DEFAULT NULL,
  `imageSource` varchar(225) DEFAULT NULL,
  `description_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `description_id` (`description_id`),
  CONSTRAINT `images_ibfk_1` FOREIGN KEY (`description_id`) REFERENCES `description` (`description_id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;

Here is the error:

   Executing (default): SELECT `id`, `pattern`, `color`, `imageUrl`, `imageSource`, `description_id`, `createdAt`, `updatedAt` FROM `images` AS `images` WHERE `images`.`pattern` = 'solid' AND `images`.`color` = 'navy-blue';
    Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'createdAt' in 'field list'
        at Query.formatError (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/lib/dialects/mysql/query.js:160:14)

2 Answers 2

9

By default, sequelize assumes that you have timestamps in your table. This can be disabled either globally

new Sequelize(..., { define: { timestamps: false }});

Or per model:

sequelize.define(name, attributes, { timestamps: false });

Or if you only have some timesstamps (fx updated, but not created)

sequelize.define(name, attributes, { createdAt: false });  

In case your column is called something else:

sequelize.define(name, attributes, { createdAt: 'make_at' });

http://docs.sequelizejs.com/en/latest/api/sequelize/

In this way, you don't have to specify all attributes each time - sequelize knows which attributes it can actually select.

If you really wanted to specify which attributes should be selected by default you could use a scope

sequelize.define(name, attributes, { defaultScope { attributes: [...] }});

And that will be applied to each find call

Sign up to request clarification or add additional context in comments.

Comments

4

You can explicitly set which attributes (column names) you want to retrieve in your query. For example, if you wanted to retrieve the id, pattern, color, imageUrl, and imageSource columns you can use the following code:

Images.findAll({
    where : {
        pattern: req.params.pattern,
        color: req.params.color
    },
    attributes : ['id', 'pattern', 'color', 'imageUrl', 'imageSource']
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.