Can I define a column name ?

1,592 views
Skip to first unread message

Philippe Jean

unread,
Jun 16, 2013, 12:37:01 PM6/16/13
to sequ...@googlegroups.com

Hi,

I'm just starting to use sequelize and it's very impressive than other orm. But I can't find in the documentation if there is a way to define the column name. All columns name in my database are in upper case with underscore but I don't want that for my model. Is there something like dbColumnName in node-persist (https://github.com/nearinfinity/node-persist) to define the mapping between the model and the column ?

Thanks

Sascha Depold

unread,
Jun 17, 2013, 1:27:54 AM6/17/13
to sequ...@googlegroups.com
Usually the column name should be the way you have defined them in the model definition:

sequelize.define('model', { name: Sequelize.STRING })

Having this, you'll get a column called "name". Can you paste in your code? 

Philippe Jean

unread,
Jun 24, 2013, 3:53:19 PM6/24/13
to sequ...@googlegroups.com
Hi,

For example, if I have a column named CODE_IBAN in the database and want to map the column to codeIBAN in the model.
It seems that there is no way to do that. I expected something like that :

sequelize.define('ACCOUNTS', {
    codeIBAN: {
        type: Sequelize.STRING,
        dbColumnName: 'CODE_IBAN'
});

Philipp Nolte

unread,
Jul 2, 2013, 10:49:10 AM7/2/13
to sequ...@googlegroups.com
sequelize.define('ACCOUNTS', {
   'CODE_IBAN': {
       type: Sequelize.STRING
   }
}, {
   timestamps
: false,
   freezeTableName
: true
});

This should do the job.

Jan Aagaard Meier

unread,
Jul 6, 2013, 6:46:55 PM7/6/13
to sequ...@googlegroups.com
Have a look at getters and setters


That will allowed you to specify CODE_IBAN in the DB structure, but have getter codeIBAN, which returns its value

Philippe Jean

unread,
Jul 12, 2013, 3:18:20 PM7/12/13
to sequ...@googlegroups.com

Thank you the link, I forgot about that possibility, but it's not enough.
When I use that, if I want to transform my instance into JSON, my object contains CODE_IBAN and not codeIBAN.
See the test case below which fails on highlighted expect  :

describe('sequelize', function(){
        var Sequelize = require('sequelize'),
            sequelize = new Sequelize('test', 'user', null, { dialect: 'sqlite' }),
            Account = sequelize.define('ACCOUNT', {
                CODE_IBAN: Sequelize.INTEGER
            }, {
                getterMethods: {
                    codeIBAN: function() {
                        return this.getDataValue('CODE_IBAN');
                    }
                },
                setterMethods: {
                    codeIBAN: function(value) {
                        return this.setDataValue('CODE_IBAN', value);
                    }
                }
            });

        it('test database column mapping', function(done){
            sequelize.sync().success(function() {
                Account.build({
                    codeIBAN: 1
                }).save().success(function(account){
                    expect(account.codeIBAN).to.be(1);
                    expect(account.values.codeIBAN).to.be(1);
                    done();
                }).error(function(err){
                    done(err);
                });
            }).error(function(err){
                done(err);
            });
        });
    });

I used different ORM in C#, Java and scala where you can map the property of your model on any column name. I'm surprised we can't do that here. Maybe, I missing something.

enen

unread,
Feb 3, 2014, 12:02:23 PM2/3/14
to sequ...@googlegroups.com
has anybody found a solution for this?

Mick Hansen

unread,
Feb 3, 2014, 12:53:10 PM2/3/14
to sequ...@googlegroups.com
virtual setters and getters are the only solution currently. We don't support being able to define a field name for an attribute.

Marty Phee

unread,
Apr 6, 2014, 5:09:24 PM4/6/14
to sequ...@googlegroups.com
This would help me also.  I'm currently writing an app whose using the same db as Hibernate application.  It would be nice to be able to specify a naming strategy like Hibernate.

I wrote a node app which actually queries the db and generates models for me.  I'd rather not generate all the get/set's
Reply all
Reply to author
Forward
0 new messages