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

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

Closed
robseward opened this issue Mar 15, 2013 · 5 comments
Closed

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

robseward opened this issue Mar 15, 2013 · 5 comments

Comments

@robseward
Copy link

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); 
        });
@dresende
Copy link
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.

@dresende
Copy link
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

@robseward
Copy link
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?

@dresende
Copy link
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.

@dresende
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants