node-orm2 icon indicating copy to clipboard operation
node-orm2 copied to clipboard

How to change the default primary_key type?

Open alanb1501 opened this issue 11 years ago • 8 comments

I'd like to change the default id type to be BigInt.

I think I can do this by specifying the id property in each object, but DRY.

alanb1501 avatar Mar 14 '14 20:03 alanb1501

Unfortunately this isn't possible at the moment as the default type is hard-coded into the database drivers themselves as INT(11) AUTO_INCREMENT (for MySQL, the equivalent for PgSQL).

notheotherben avatar Mar 14 '14 21:03 notheotherben

self.setEmotionalState(SAD_PANDA);

alanb1501 avatar Mar 14 '14 21:03 alanb1501

My suggestion (for the time being) would be to make changes to the node_modules/node-sql-ddl-sync/lib/Dialects/mysql.js file to reflect the datatype you wish to use - keep in mind that you will need to change it each time you update node-orm2.

notheotherben avatar Mar 14 '14 21:03 notheotherben

There has been some work done to add support for this, but it's not quite complete. The idea is to have:

db.define("person", {
  identifier: { type: 'string', key: true}
});

If the table was created outside of ORM, it might just work as is.

dxg avatar Mar 17 '14 08:03 dxg

Any updates on this?

louy avatar Aug 17 '15 09:08 louy

Just found a way to do it.

orm.define('city', {
  id: { 
    type: 'text', 
    required: true, 
    size: 100, // important or relationships won't work
  },
  name: String,
}, {
  id: 'id', // important
});

louy avatar Aug 17 '15 09:08 louy

Also try this (key: true):

orm.define('city', {
  id: { 
    type: 'text', 
    required: true, 
    size: 100, // important or relationships won't work
    key: true
  },
  name: String,
});

dxg avatar Aug 17 '15 09:08 dxg

Oh, so key stands for PK? I didn't know that. It worked. Thanks. It generates the table correctly, but we shouldn't really need the size property.

louy avatar Aug 17 '15 09:08 louy