dresende / node-orm2

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

How to change the default primary_key type? #474

Open alanb1501 opened 10 years ago

alanb1501 commented 10 years ago

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.

notheotherben commented 10 years ago

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).

alanb1501 commented 10 years ago

self.setEmotionalState(SAD_PANDA);

notheotherben commented 10 years ago

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.

dxg commented 10 years ago

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.

louy commented 9 years ago

Any updates on this?

louy commented 9 years ago

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
});
dxg commented 9 years ago

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,
});
louy commented 9 years ago

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.