Skip to content

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

Closed

Description

@takempf

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.

Activity

  1. dresende commented on Jul 30, 2013

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

  2. dresende commented on Jul 30, 2013

    @dresende
    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.
  3. dresende commented on Jul 30, 2013

    @dresende
    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!
    });
  4. dresende commented on Jul 30, 2013

    @dresende
    Owner

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

  5. takempf commented on Jul 30, 2013

    @takempf
    Author

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

  6. takempf commented on Jul 31, 2013

    @takempf
    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?

  7. dresende commented on Jul 31, 2013

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions