dresende / node-orm2

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

option big doesn't work with model field type 'text' in MySQL #521

Open neekey opened 10 years ago

neekey commented 10 years ago

below is my model definition:

MockData  = db.define('mock_data', {
            name:{
                type: 'text',
                size: 128,
                required: true
            },
            data: {
                type: 'text',
                required: true,
                defaultValue: '{}',
                big: true
            },
            isActive: {
                type: 'boolean',
                required: true,
                defaultValue: false
            },
            query: {
                type: 'text',
                defaultValue: '?',
                required: true
            }
        },{
            cache: false,
            validations:{
                name:function(value,next,ctx){
                    next();
                }
            },
            hooks:{
            }
        });
        MockData.hasOne( 'schema', schema, { reverse:'schema_id' } );
    }

i use data to store very long data string, so i use big ( by default orm2 just set varchar(255) for text ), but when I set big to true, orm2 just doesn't create the table for me, if remove big it works fine.

mike-zenith commented 10 years ago

According to my test (using db.sync) and sql-ddl-sync/lib/Dialects/mysql.js, it creates longtext.

package.json

    "mysql": "~2.0.0-alpha9",
    "orm": "^2.1.15",

model definition in badge.js

    db.define('badge', {
        name: { type: 'text', big:true },
        image: { type: 'text', size: 128 }
    }, {
        methods: {
        }
    });

result using connection.debug

(orm/mysql) CREATE TABLE `badge` (`name` LONGTEXT, `image` VARCHAR(128), `id` INT(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`))
neekey commented 10 years ago

@mike-zenith update to orm@2.1.15 gets it work, but encountered another problem for me : field type text with big option can not have defaultValue

dxg commented 10 years ago

According to the [mysql doco]:

BLOB and TEXT columns cannot have DEFAULT values. so this is a mysql limitation.

Unfortunately, orm doesn't discriminate between database set default values & ones set in the javascript code so default values for big text with mysql will fail. I recommend Postgres.