Skip to content

Default values is not used when property is not specified #104

@freemanrnd

Description

@freemanrnd

Test example:

    var TestEntity = db.define('testentity', {
      name: {type: 'text', defaultValue: "Default name"},
      rating: {type: 'number', rational: false, defaultValue: 0}
    });

    db.sync();

    TestEntity.create([
      {name: "Test name"},
      {rating: 15},
      {}
    ], function(err, items){
      if(err){
        throw err;
      }
      console.log("ok");
    });
$ node app.js
ok

mysql> SELECT * FROM testentity;
+----+-----------+--------+
| id | name      | rating |
+----+-----------+--------+
|  1 | Test name |   NULL |
|  2 | NULL      |     15 |
|  3 | NULL      |   NULL |
+----+-----------+--------+

mysql> describe testentity;
+--------+------------------+------+-----+--------------+----------------+
| Field  | Type             | Null | Key | Default      | Extra          |
+--------+------------------+------+-----+--------------+----------------+
| id     | int(11) unsigned | NO   | PRI | NULL         | auto_increment |
| name   | varchar(255)     | YES  |     | Default name |                |
| rating | int(11)          | YES  |     | 0            |                |
+--------+------------------+------+-----+--------------+----------------+

As you can see sync() works correctly and creates proper table structure containing default values. Therefore I assume that inserting query contains statement with explicit NULL assignement.

I think that orm should use default value if property of entity were not specified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions