Skip to content

Models that fail a save to the DB still save to the cache #78

Closed

Description

@robseward

Hi, I'm not sure if this is by design or not, but I noticed if a model fails validation during save, it is still saved to the cache. I'm using postgres.

Model definition

module.exports = function(db){
    var Project = db.define("projects", {
        name      : String,
        created   : Date,
        project_status  : ['active', 'completed', 'on hold', 'dropped']
    }, {
        methods: {
        },
        validations: {
            project_status: orm.validators.insideList([ 'active', 'completed', 'on hold', 'dropped' ], "Invalid Status")
        },
        cache: true
    });

    Project.hasOne('user');
    return Project;
};

Save an invalid value to the model.

Project.get(1, function(err, project){
            project.project_status = 'foo';
            project.save(function(err){

//This will print a validation error
                console.log(err);
            });

        });

Later, retrieving the model will print the invalid value.

Project.get(1, function(err, project){
            //This will print 'foo'
            console.log(project.project_status); 
        });

Activity

  1. dresende commented on Mar 15, 2013

    @dresende
    Owner

    This is somewhat the expected behaviour, I'm not sure the best strategy to avoid this. If you do this on the second Person.get:

    Project.get(1, function(err, project){
        console.log(project.saved()); // false
    });

    Perhaps I can force the Singleton object to fetch from database when cached instance is not saved. What do you think?

    This made me think that an Instance.revert() might also come in handy.

  2. dresende commented on Mar 15, 2013

    @dresende
    Owner

    What do you think? If you use the last git version your problem should be solved. Still, for people wanting the old behaviour, it's just a setting away.

    var orm = require("orm");
    
    orm.settings.set("instance.cacheSaveCheck", false); // true by default
  3. robseward commented on Mar 16, 2013

    @robseward
    Author

    Looks good to me. Let me see if I understand. You enabled cacheSaveCheck to be true by default, and now saving to cache will check to see if it was saved to the cache first?

  4. dresende commented on Mar 17, 2013

    @dresende
    Owner

    I created cacheSaveCheck that if enabled (default) will not return an instance from cache if it's not saved. So if you fetch A, change it, and then fetch again, the cache will see that what it has is not saved and so will fetch it again.

  5. dresende commented on Mar 19, 2013

    @dresende
    Owner

    I'll assume this now behaves as you expected. If not please reopen and explain :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions