dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

Main example does not work with sqlite #622

Closed DarkPark closed 9 years ago

DarkPark commented 9 years ago

The example from the index page with switching to sqlite in-memory database and error logging:

var orm = require("orm");

orm.connect({protocol: 'sqlite'}, function (err, db) {
    if (err) throw err;

    var Person = db.define("person", {
        name      : String,
        surname   : String,
        age       : Number, // FLOAT
        male      : Boolean,
        continent : [ "Europe", "America", "Asia", "Africa", "Australia", "Antartica" ], // ENUM type
        photo     : Buffer, // BLOB/BINARY
        data      : Object // JSON encoded
    }, {
        methods: {
            fullName: function () {
                return this.name + ' ' + this.surname;
            }
        },
        validations: {
            age: orm.enforce.ranges.number(18, undefined, "under-age")
        }
    });

    Person.find({ surname: "Doe" }, function (err, people) {
        // SQL: "SELECT * FROM person WHERE surname = 'Doe'"

        console.log(err);

        console.log("People found: %d", people.length);
        console.log("First person: %s, age %d", people[0].fullName(), people[0].age);

        people[0].age = 16;
        people[0].save(function (err) {
            // err.msg = "under-age";
        });
    });
});

gives:

node server/orm.js 
{ [Error: SQLITE_ERROR: no such table: person] errno: 1, code: 'SQLITE_ERROR' }

/home/dp/Projects/FortNotes/server/orm.js:77
                console.log("People found: %d", people.length);
                                                      ^
TypeError: Cannot read property 'length' of undefined
    at /home/dp/Projects/FortNotes/server/orm.js:77:41
    at /home/dp/Projects/FortNotes/node_modules/orm/lib/ChainFind.js:201:13
    at Statement.errBack (/home/dp/Projects/FortNotes/node_modules/sqlite3/lib/sqlite3.js:16:21)

In case I use the existing db file with this connect line:

orm.connect('sqlite:///home/dp/Projects/FortNotes/server/db/sqlite/db.sqlite', function (err, db) {
...

and add empty table person then it fails in a different way:

{ [Error: SQLITE_ERROR: no such column: name] errno: 1, code: 'SQLITE_ERROR' }

/home/dp/Projects/FortNotes/server/orm.js:80
                console.log("People found: %d", people.length);
                                                      ^
TypeError: Cannot read property 'length' of undefined
    at /home/dp/Projects/FortNotes/server/orm.js:80:41
    at /home/dp/Projects/FortNotes/node_modules/orm/lib/ChainFind.js:201:13
    at Statement.errBack (/home/dp/Projects/FortNotes/node_modules/sqlite3/lib/sqlite3.js:16:21)

Seems to me this example should define a table, it's structure and actually create it in the db file. Am I wrong and I have to manually create all the tables with all the fields?

staff0rd commented 9 years ago

The example does not sync the model (which you have resolved by adding the empty table), nor does it populate the table with any data (which is the step you are missing).

I've extended the example to do both of those and tested with sqlite & sent a pull request. You can see the change here.

Switching the Person.find block with the following does both the sync and the create;

// add the table to the database
db.sync(function(err) { 
    if (err) throw err;

    // add a row to the person table
    Person.create({ id: 1, name: "John", surname: "Doe", age: 27 }, function(err) {
        if (err) throw err;

            // query the person table by surname
            Person.find({ surname: "Doe" }, function (err, people) {
                // SQL: "SELECT * FROM person WHERE surname = 'Doe'"
                if (err) throw err;

                console.log("People found: %d", people.length);
                console.log("First person: %s, age %d", people[0].fullName(), people[0].age);

                people[0].age = 16;
                people[0].save(function (err) {
                    // err.msg = "under-age";
            });
        });

    });
});
DarkPark commented 9 years ago

@staff0rd thank you very much - this time it really works sad that official documentation provides misleading examples