dresende / node-orm2

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

findByAssociation and custom `key` doesn't work #657

Open louy opened 8 years ago

louy commented 8 years ago

I've been getting the following error when using findByAssociation:

  Error: ER_BAD_FIELD_ERROR: Unknown column 't1._id' in 'on clause'

The main reason is that I have orm.settings.set('properties.primary_key', '_id'); while in one table, I have id: { type: 'text', key: true, size: 100, },. I'm not sure where the issue is but it's probably in the mysql driver.

dxg commented 8 years ago

It's probably here. I previously added support for mapsTo and then patched some bugs related to it. I don't remember patching the findBy function though.

louy commented 8 years ago

Doesn't it mean that the issue is that association.model.id (or maybe association.field) is incorrectly set? I'm not sure what mapsTo stands for.

dxg commented 8 years ago

Setting orm.settings.set('properties.primary_key', '_id') won't help you here. It's just the name of the default primary key property that's added to models if you don't add one yourself.

Since you have id: { type: 'text', key: true, size: 100 }, you need to tell ORM that the column in the database has a different name. You can do this by adding mapsTo. So id in the model mapsTo _id in the table.

id: { type: 'text', key: true, size: 100, mapsTo: '_id' }

That said, im not sure if that'll fix your problems as I can't remember if I ever fixed finyBy.. so that it supports mapsTo.

reumia commented 6 years ago

@dxg it seems to be not fixed. findBy... can't supports mapsTo yet

I opened an issue on #817 about this problem. Please check this :(