UnQLite Users Forum

Schemas Do Not Work

append delete c1moore

I have some similar code for creating a collection and a schema.

%
if(!db_exists('collectionName') {
    $result = db_create('collectionName');
    if(!$result) {
        print "Error: Could not create collection."

        return;
    }

    $schema = {
        _id:        'integer',
        name:    'string',
        data:      'array'
    }

    $result = db_set_schema('collectionName', $schema);
    if(!$result) {
        print db_errlog();
        db_rollback();

        return;
    }
}
%

After executing, the error %Collection 'collectionName' not defined in the underlying database% is printed. Removing the section of code dealing with the schema appears to work.

Reply RSS

Replies

append delete #1. chm

CollectionName is a reserved keyword. Try to use another one to avoid underlying conflicts.

append delete #2. c1moore

CollectionName was an example. I am passing in a variable to the db_exists function. For testing, this value is actually 'fuelLevel'.

append delete #3. chm

Ah OK, forget to mention also the _id is a reserved keyword, simply use id (without _) and try to commit changes to disk after creating the collection and see the result.

%Jx9

if(!db_exists('collectionName') {
    $result = db_create('collectionName');
    if(!$result) {
        print "Error: Could not create collection."

        return;
    }
  
 db_commit(); //Make sure the collection is created

    $schema = {
        id:        'integer', //avoid __
        name:    'string',
        data:      'array'
    }

    $result = db_set_schema('collectionName', $schema);
    if(!$result) {
        print db_errlog();
        db_rollback();

        return;
    }
}
%
append delete #4. c1moore

Removing the id field completely and only using the builtin id does not help. I still get the same problem. So my code looks similar to this:

%Jx9


if(!db_exists('collectionName') {
    $result = db_create('collectionName');
    if(!$result) {
        print "Error: Could not create collection."

        return;
    }
  
 db_commit(); //Make sure the collection is created

    $schema = {
        name:    'string',
        data:      'array'
    }

    $result = db_set_schema('collectionName', $schema);
    if(!$result) {
        print db_errlog();
        db_rollback();

        return;
    }
}
%

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: chm_at_symisc, devel_at_symisc