What am I doing wrong with Schema.newRecord()?

19 views
Skip to first unread message

Eric Dykstra

unread,
Aug 30, 2013, 9:47:38 AM8/30/13
to sil...@googlegroups.com
Everyone,

I have the following table:

mysql> SELECT * FROM Churches_Clubs WHERE church_club_church_id = 1;
+----------------+-----------------------+---------------------+
| church_club_id | church_club_church_id | church_club_club_id |
+----------------+-----------------------+---------------------+
|              1 |                     1 |                   1 |
|              6 |                     1 |                   4 |
|              7 |                     1 |                   5 |
+----------------+-----------------------+---------------------+

I want to add a row with:

church_club_church_id = 1
church_club_club_id = 2

In the file /rpc/Churches_Clubs.sjs, I have:

    newRecord: function(example) { return Schema.newRecord('Churches_Clubs', example); },
    
    putOne:    function(example) { 

        console.log('Churches_Clubs.sjs : putOne : example =');
        console.dir(example);

        var church_club = {};

        church_club.extend({
            church_club_id:         0,
            church_club_church_id:  example.church_club_church_id,
            church_club_club_id:    example.church_club_club_id
        });

        church_club = Churches_Clubs.newRecord(church_club);

        console.log('Churches_Clubs.sjs : putOne : church_club =');
        console.dir(church_club);    

        return church_club;
    }

The output of the above is:

 Churches_Clubs.sjs : putOne : example =
/var/www/acs/rpc/Churches_Clubs.sjs line 89 (exports.putOne):
 (object) :
 [church_club_church_id] : (number) 1
 [church_club_club_id] : (number) 2

Churches_Clubs.sjs : putOne : church_club =
/var/www/acs/rpc/Churches_Clubs.sjs line 102 (exports.putOne):
 (object) :
 [church_club_id] : (number) 0
 [church_club_church_id] : (number) 1
 [church_club_club_id] : (number) 2

It is pretty obvious from the info above, but for completeness, here is the schema definition:

Schema.define({
    name:       'Churches_Clubs',
    fields:      [
        { name: 'church_club_id',                      type: 'int',     autoIncrement: true, defaultValue: 1 },
        { name: 'church_club_church_id',               type: 'int'                                           },
        { name: 'church_club_club_id',                 type: 'int'                                           }
    ],
    indexes:     ['church_club_church_id', 'church_club_club_id'],
    primaryKey: 'church_club_id'
});

So, WHY after executing 'putOne' and therefore specifically, this line from above;

church_club = Schema.newRecord('Churches_Clubs', church_club);

do I still have the same records in the table as shown below?

mysql> SELECT * FROM Churches_Clubs WHERE church_club_church_id = 1;
+----------------+-----------------------+---------------------+
| church_club_id | church_club_church_id | church_club_club_id |
+----------------+-----------------------+---------------------+
|              1 |                     1 |                   1 |
|              6 |                     1 |                   4 |
|              7 |                     1 |                   5 |
+----------------+-----------------------+---------------------+

I fear I am doing (or not doing) something really 'stupid', and can't see it. I am hoping extra eye on this will find it, because I need to stop pulling out the little hair that I have left. So - please - just point it out and don't worry about making me look like a fool. :)

Thanks,

Eric


Michael Schwartz

unread,
Aug 30, 2013, 9:54:46 AM8/30/13
to sil...@googlegroups.com
Schema.newRecord() simply populates a JavaScript object with the default values from your schema definition.  

var newRec = Schema.newRecord('Churches_Clubs', { override: with_this });

You will simply get a record like:

{ church_club_id: 0, church_club_church_id: 0, church_club_club_id: 0 }

When you do a putOne('Churches_Clubs', newRec), it does a REPLACE INTO query.  

Since the primary key is 0, it will use the autoIncrement value in the DB as the primary key and insert the record.  

If you call putOne() with an object that has primaryKey set, it will use that instead of allocating one autoIncrement automatically.  This appears to be what you're doing (wrong).

After calling putOne(), you can examine newRec to find church_club_id has been set to the inserted ID.  It's smart :)  You will find this useful for cases where you call putOne() to 3 different joined tables.




--
You received this message because you are subscribed to the Google Groups "SilkJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to silkjs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Michael Schwartz

unread,
Aug 30, 2013, 9:59:08 AM8/30/13
to sil...@googlegroups.com
I forgot to mention that the override overrides the default values that newRecord returns.  So in your Churches_Clubs.sjs, newRecord method…

The example parameter may be a full or partially defined object/record of schema type Churches_Clubs.  The newRecord() method you defined will create an object with default values and then replace any values in that with the ones from your example.

On Aug 30, 2013, at 6:47 AM, Eric Dykstra <virtu...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages