Repository navigation
Setting model created with 'extendsTo' fails with 'ER_NO_DEFAULT_FOR_FIELD' #250
Copy link
Copy link
Closed
Labels
Description
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.
Reactions are currently unavailable
Activity
dresende commentedon Jul 30, 2013
I'm getting a bad query to update
providerswhen callingthis.save()inafterCreatehook but not that error. Please try the latest git version.Allows user to pass an object to extendsTo.setAccessor instead of an …
dresende commentedon Jul 30, 2013
Get the latest commit and change your code to this:
extendsToreturn Model. Accessors are created inRoom(setPlayer,getPlayer, ..);dresende commentedon Jul 30, 2013
And don't forget:
extendsTois just an extension of a Model, it does not allow you to have manyRoomPlayersin oneRoom. For that you need to do:dresende commentedon Jul 30, 2013
Well, the bug I thought existed just vanished with my change 😄
takempf commentedon Jul 30, 2013
I'm going to try this out tonight, I'll get back to you on this in about 8 hours...
takempf commentedon Jul 31, 2013
Looks like both
before*hook transformation and model extension work now. It seems like there are quite a few major unreleased fixes inmaster... is there any reason these haven't been added to a release yet?dresende commentedon Jul 31, 2013
I'm going to release it in the end of the week.