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

findByAssociation and custom `key` doesn't work

Open louy opened this issue 10 years ago • 4 comments

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.

louy avatar Aug 18 '15 11:08 louy

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.

dxg avatar Aug 19 '15 07:08 dxg

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.

louy avatar Aug 19 '15 07:08 louy

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.

dxg avatar Aug 19 '15 15:08 dxg

@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 :(

reumia avatar Oct 19 '17 06:10 reumia