Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequelize is trying to update a virtual field #2860

Closed
GuilhermeReda opened this issue Jan 8, 2015 · 3 comments
Closed

Sequelize is trying to update a virtual field #2860

GuilhermeReda opened this issue Jan 8, 2015 · 3 comments
Assignees

Comments

@GuilhermeReda
Copy link
Contributor

Hi, I have this code

var Sequelize = require('sequelize');
var config  = require(__dirname + '/config/database.json')['production'];
var sequelize = new Sequelize(config.database, config.username, config.password, config);

var User = sequelize.define("User", {

  login: {
    type      : Sequelize.STRING,
    unique    : true,
    allowNull   : false,
    validate    : {
    len     : {
      args: [6, Infinity],
      msg: 'Login is too short'
    },
    isUnique  : function(value, next) {

      User.find({
      where: Sequelize.and({login: value}, ['id <> ?', this.id]),
      attributes: ['login']
      })
      .then(function(pf) {
      if (pf)
        return next('User is taken!');

      next();

      })
      .catch(function(err) {

      return next(err);

      });

    }
    }
  },

  pass: {
    type      : Sequelize.STRING,
    allowNull   : false,
    validate    : {
    len     : {
      args: [6, Infinity],
      msg: 'A senha é muito curta. min 6 characteres'
    }
    }
  },

  check_pass: {
    type      : Sequelize.VIRTUAL,
    allowNull   : false,
    validate    : {
    match     : function (val) {
      if (val !== this.check_pass) {
      throw new Error('Wrong pass.');
      }
    }
    }
  },

  });

sequelize.sync().then(function () {

  var data = {
    id: 1,
    login: 'testing',
    pass: 'hashed_pass_here',
    check_pass: 'hashed_pass_here'
  };

  var where = {
    id: 1
  }

  User
    .create(data)
    .then(function(result) {
      console.log('User created'); // IT WORKS
      User
        .update(data, {where: where})
        .then(function(result) {
          console.log('User updated:', result); 
        })
        .catch(function(err) {
          console.log('Error updating user', err, err.stack); // IT GOES HERE
        });
    })
    .catch(function(err) {
      console.log('Error creating user', err, err.stack);
    })
});

Output:


Executing (default): SELECT "login" FROM "Users" AS "User" WHERE ("User"."login"='testing' AND id <> 1) LIMIT 1;
Executing (default): INSERT INTO "Users" ("id","login","pass","createdAt","updatedAt") VALUES (1,'testing','hashed_pass_here','2015-01-08 08:25:33.120 -03:00','2015-01-08 08:25:33.120 -03:00') RETURNING *;
User created
Executing (default): SELECT "login" FROM "Users" AS "User" WHERE ("User"."login"='testing' AND id <> 1) LIMIT 1;
Executing (default): UPDATE "Users" SET "id"=1,"login"='testing',"pass"='hashed_pass_here',"check_pass"='hashed_pass_here',"updatedAt"='2015-01-08 08:25:33.191-03:00' WHERE "id"=1
Error updating user { [SequelizeDatabaseError: coluna "check_pass" da relação "Users" não existe] // means Column "check_pass" of relation "Users" does not exists 
  name: 'SequelizeDatabaseError',
  message: 'coluna "check_pass" da relação "Users" não existe', // means Column "check_pass" of relation "Users" does not exists 
  parent:
   { [error: coluna "check_pass" da relação "Users" não existe] // means Column "check_pass" of relation "Users" does not exists 
     name: 'error',
     length: 133,
     severity: 'ERRO',
     code: '42703',
     detail: undefined,
     hint: undefined,
     position: '71',
     internalPosition: undefined,
     internalQuery: undefined,
     where: undefined,
     file: 'src\\backend\\parser\\analyze.c',
     line: '2001',
     routine: 'transformUpdateStmt',
     sql: 'UPDATE "Users" SET "id"=1,"login"=\'testing\',"pass"=\'hashed_pass_here\',"check_pass"=\'hashed_pass_here\',"updatedAt"=\'2015-01-08 08:25:33.191 -03:00\' WHERE "id"=1' },
  original:
   { [error: coluna "check_pass" da relação "Users" não existe] // means Column "check_pass" of relation "Users" does not exists 
     name: 'error',
     length: 133,
     severity: 'ERRO',
     code: '42703',
     detail: undefined,
     hint: undefined,
     position: '71',
     internalPosition: undefined,
     internalQuery: undefined,
     where: undefined,
     file: 'src\\backend\\parser\\analyze.c',
     line: '2001',
     routine: 'transformUpdateStmt',
     sql: 'UPDATE "Users" SET "id"=1,"login"=\'testing\',"pass"=\'hashed_pass_here\',"check_pass"=\'hashed_pass_here\',"updatedAt"=\'2015-01-08 08:25:33.191 -03:00\' WHERE "id"=1' },
  sql: 'UPDATE "Users" SET "id"=1,"login"=\'testing\',"pass"=\'hashed_pass_here\',"check_pass"=\'hashed_pass_here\',"updatedAt"=\'2015-01-08 08:25:33.191 -03:00\' WHERE "id"=1' } 

SequelizeDatabaseError: coluna "check_pass" da relação "Users" não existe // means Column "check_pass" of relation "Users" does not exists 
    at module.exports.Query.formatError (C:\Users\Guilherme\Documents\node\iapo1\node_modules\sequelize\lib\dialects\postgres\query.js:301:16)
    at null.<anonymous> (C:\Users\Guilherme\Documents\node\iapo1\node_modules\sequelize\lib\dialects\postgres\query.js:64:21)
    at emit (events.js:95:17)
    at Query.handleError (C:\Users\Guilherme\Documents\node\iapo1\node_modules\pg\lib\query.js:99:8)
    at null.<anonymous> (C:\Users\Guilherme\Documents\node\iapo1\node_modules\pg\lib\client.js:166:26)
    at emit (events.js:95:17)
    at Socket.<anonymous> (C:\Users\Guilherme\Documents\node\iapo1\node_modules\pg\lib\connection.js:109:12)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)

It validates to check if pass is equal check_pass and insert, it does the validation again on update but it doesn't remove check_field before trying to update it.

@gtomitsuka
Copy link

Yup, I'm having just the same issue...

@janmeier janmeier self-assigned this Apr 8, 2015
@NeroZonbolt
Copy link

NeroZonbolt commented Jul 16, 2024

Is it not working yet?

Got this error a min ago...
image

Model
image

Code that triggered the virtual field to crash
image

@gtomitsuka
Copy link

Probably unrelated since it's been 9 years since it was reported lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants