coresmart / persistencejs

persistence.js is an asynchronous Javascript database mapper library. You can use it in the browser, as well on the server (and you can share data models between them).
http://persistencejs.org
1.73k stars 240 forks source link

Model with several relations won't create #150

Closed AleksMeshkov closed 10 years ago

AleksMeshkov commented 10 years ago

Hi! Below is a simple function that creates specialist with 3 relations: phones, tags and emails. Somehow this code doesn't work. At the same time if I comment that 3 for loops everything runs ok.

addSpecialist       : function (model, emails, phones, tags) {
                var s = new Specialist();
                s.fullName = model.fullName;
                s.photoUrl = model.photoUrl;
                s.address = model.address;
                persistence.add(s);

                for (var i = 0 ; i < tags.length ; i++) {
                    s.tags.add(new Tag({tag : tags[i].tag}));
                }

                for (var i = 0 ; i < emails.length ; i++) {
                    s.emails.add(new Email({email : emails[i].email}));
                }

                for (var i = 0 ; i < phones.length ; i++) {
                    s.phones.add(new Phone({number : phones[i].phone}));
                }

                persistence.flush();
            }

Here my persistencejs schema config:

persistence.store.cordovasql.config(
            persistence,
            'TryOrShy',
            '0.0.1',                // DB version
            'TryOrShy database',          // DB display name
            20 * 1024 * 1024,        // DB size
            1                       // SQLitePlugin Background processing enabled
        );

        var Specialist = persistence.define('Specialist', {
            fullName  : 'TEXT',
            firstName : 'TEXT',
            lastName  : 'TEXT',
            photoUrl  : 'TEXT',
            address   : 'TEXT'
        });

        var Phone = persistence.define('Phone', {
            number : 'TEXT'
        });

        var Email = persistence.define('Email', {
            email : 'TEXT'
        });

        var Tag = persistence.define('Tag', {
            tag : 'TEXT'
        });

        // Relations
        Specialist.hasMany('phones', Phone, 'specialist');
        Specialist.hasMany('emails', Email, 'specialist');
        Specialist.hasMany('tags', Tag, 'specialist');

        // Indexes
        Specialist.index(['fullName', 'firstName', 'lastName']);
        Tag.index('tag');
        Email.index('email', {unique : true});
        Phone.index('number');

        persistence.schemaSync();

Could I you please help me with this issue? Thanks in advance!

AleksMeshkov commented 10 years ago

Maybe I have problems with indexes? Tried to comment them. Think it's fine for now.

AleksMeshkov commented 10 years ago

Doesn't work on Android 4.4.2 Is it somehow related to Cordova-SQLitePlugin?

zefhemel commented 10 years ago

It probably helps to be more specific when you say "it doesn't work" Do you get an error? Does something not happen that you expect to happen?

AleksMeshkov commented 10 years ago

@zefhemel, hey, thankss for reply. I didn't get any errors. Console.log showed me that anything went well. I gave up using Cordova-SQLitePlugin and everything became ok.