You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
targetKey not working in BelongsTo association (sync() problem?) #4455
The query generated by sync() ignores the targetKey configuration:
CREATETABLEIF NOT EXISTS "photos" (
"id"SERIAL ,
"location"VARCHAR(255),
"itemId"INTEGERREFERENCES"users" ("id") ON DELETESETNULLONUPDATE CASCADE,
"createdAt"TIMESTAMP WITH TIME ZONENOT NULL,
"updatedAt"TIMESTAMP WITH TIME ZONENOT NULL,
PRIMARY KEY ("id")
);
You can see it references id instead of uid, as intended.
If, on the contrary, we specify {references: {model: User, key: 'uid'}} when calling sequelize.define(), the relationship is created by sync() correctly. In this case, the relationship also works correctly when e.g. creating a photo and calling getUser() on it, so I guess the problem is in the implementation of sync().
As a side note, when will targetKey be supported for HasMany relationships? It seems to me a very important feature.
I can also confirm that, in the example above, finding Photos and includeing the corresponding User works as expected (when {references: {model: User, key: 'uid'}} is used).
Interesting. Looking at the test, I find the following line (I don't know why the id is removed in this test):
User.removeAttribute('id');
If I do that in my case, the FK constraint is not created at all (this is not verified in the test):
CREATETABLEIF NOT EXISTS "photos" (
"id"SERIAL ,
"location"VARCHAR(255),
"itemId"INTEGER,
"createdAt"TIMESTAMP WITH TIME ZONENOT NULL,
"updatedAt"TIMESTAMP WITH TIME ZONENOT NULL,
PRIMARY KEY ("id")
);
...and getting the User from a created Photo works correctly, just as in the test.
Activity
guigrpa commentedon Sep 8, 2015
See also #4269 and #4258
guigrpa commentedon Sep 8, 2015
I can also confirm that, in the example above, finding
Photosandincludeing the correspondingUserworks as expected (when{references: {model: User, key: 'uid'}}is used).mickhansen commentedon Sep 8, 2015
We have a working test here:
sequelize/test/integration/include/find.test.js
Line 173 in ea63814
guigrpa commentedon Sep 8, 2015
Interesting. Looking at the test, I find the following line (I don't know why the
idis removed in this test):If I do that in my case, the FK constraint is not created at all (this is not verified in the test):
...and getting the
Userfrom a createdPhotoworks correctly, just as in the test.mickhansen commentedon Sep 8, 2015
Hmm, curious if it works without removing the primary key, maybe it doesn't.
jayfresh commentedon Sep 15, 2015
I also stumbled into this problem. Removing the id from the target removes the error, but I also fail to see the relation created in the SQL.
mickhansen commentedon Sep 15, 2015
Looks like the test was written like that because SQLITE does not support constraints on anything other than primary key.
mickhansen commentedon Sep 15, 2015
Ah well, neither does postgres, looks like there shouldn't be a constraint at all then (unless it's a unique).