Skip to content

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

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

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