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

Setting model created with 'extendsTo' fails with 'ER_NO_DEFAULT_FOR_FIELD' #250

Closed
Fauntleroy opened this issue Jul 28, 2013 · 7 comments
Closed

Comments

@Fauntleroy
Copy link

No matter what I try to do, I can't seem to get past this error when I attempt to use RoomPlayer.create:

{
  "code": "ER_NO_DEFAULT_FOR_FIELD",
  "index": 0,
  "instance": {
    "order": "normal",
    "max_duration": 60,
    "providers": null,
    "item": null
  }
}

My room model is as follows:

var orm = require('orm');
var slug = require('slugg');

module.exports = function( db ){

    var User = db.models.user;

    var Room = db.define( 'room', {
        title: {
            type: 'text',
            required: true,
            unique: true
        },
        slug: String,
        subtitle: {
            type: 'text',
            defaultValue: 'A great place to share with friends!'
        },
        active: Boolean,
        'private': Boolean
    }, {
        hooks: {
            beforeSave: function( next ){
                this.slug = slug( this.title );
                next();
            }
        }
    });

    var RoomPlayer = Room.extendsTo( 'player', {
        order: {
            type: 'text',
            defaultValue: 'normal'
        },
        max_duration: {
            type: 'number',
            defaultValue: 60 // minutes
        },
        providers: Object,
        item: Object
    }, {
        hooks: {
            afterCreate: function( success ){
                if( !success ) return;
                this.save({
                    providers: {
                        youtube: true,
                        soundcloud: true,
                        vimeo: true
                    }
                });
            }
        }
    });

    Room.hasOne( 'creator', User, {
        reverse: 'rooms'
    });

    Room.start = function( data, user_id, cb ){
        cb = cb || function(){};
        User.get( user_id, function( err, user ){
            if( !user ) return cb( err || new Error('User could not be found.'), null );;
            Room.create( data, function( err, room ){
                if( !room ) return cb( err || new Error('Room could not be created.'), null );
                room.setCreator( user, function( err ){
                    if( err ) return cb( err, null );
                    RoomPlayer.create( {}, function( err, player ){
                        if( err ) return cb( err, null );
                        room.setPlayer( player, function( err ){
                            if( err ) return cb( err );
                            return cb( null, room );
                        });
                    });
                });
            });
        });
    };

    return Room;

};

My first guess is that I'm just doing something wrong, but the documentation for extendsTo doesn't clearly illustrate how to use the extended model.

@dresende
Copy link
Owner

I'm getting a bad query to update providers when calling this.save() in afterCreate hook but not that error. Please try the latest git version.

dresende added a commit that referenced this issue Jul 30, 2013
@dresende
Copy link
Owner

Get the latest commit and change your code to this:

var orm = require('orm');
var slug = require('slugg');

module.exports = function( db ){

    var User = db.models.user;

    var Room = db.define( 'room', {
        title: {
            type: 'text',
            required: true,
            unique: true
        },
        slug: String,
        subtitle: {
            type: 'text',
            defaultValue: 'A great place to share with friends!'
        },
        active: Boolean,
        'private': Boolean
    }, {
        hooks: {
            beforeSave: function( next ){
                this.slug = slug( this.title );
                next();
            }
        }
    });

    Room.extendsTo( 'player', {
        order: {
            type: 'text',
            defaultValue: 'normal'
        },
        max_duration: {
            type: 'number',
            defaultValue: 60 // minutes
        },
        providers: Object,
        item: Object
    });

    Room.hasOne( 'creator', User, {
        reverse: 'rooms'
    });

    Room.start = function( data, user_id, cb ){
        cb = cb || function(){};
        User.get( user_id, function( err, user ){
            if( !user ) return cb( err || new Error('User could not be found.'), null );;
            Room.create( data, function( err, room ){
                if( !room ) return cb( err || new Error('Room could not be created.'), null );
                room.setCreator( user, function( err ){
                    if( err ) return cb( err, null );

                    var roomPlayer = {
                        providers: {
                            youtube: true,
                            soundcloud: true,
                            vimeo: true
                        }
                    };
                    room.setPlayer( roomPlayer, function( err ){
                        return cb( null, room );
                    });
                });
            });
        });
    };

    return Room;
};
  • You don't need to save the extendsTo return Model. Accessors are created in Room (setPlayer, getPlayer, ..);
  • No need to have the hook, although you could but there seems to be a bug somewhere, I have to find it.

@dresende
Copy link
Owner

And don't forget: extendsTo is just an extension of a Model, it does not allow you to have many RoomPlayers in one Room. For that you need to do:

Room.hasMany("players", User, {
    order: ...,
    max_duration: ...
});

// ...
some_room.addPlayers(user1, user2, ..., function () {
    // added!
});

@dresende
Copy link
Owner

Well, the bug I thought existed just vanished with my change 😄

@Fauntleroy
Copy link
Author

I'm going to try this out tonight, I'll get back to you on this in about 8 hours...

@Fauntleroy
Copy link
Author

Looks like both before* hook transformation and model extension work now. It seems like there are quite a few major unreleased fixes in master... is there any reason these haven't been added to a release yet?

@dresende
Copy link
Owner

I'm going to release it in the end of the week.

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

No branches or pull requests

2 participants