Skip to content

targetKey not working in BelongsTo association (sync() problem?) #4455

Closed

Description

@guigrpa

Consider this very simple case (under Sequelize v3.7.1):

User = sequelize.define 'user',
  name: Sequelize.STRING
  uid: 
    type: Sequelize.INTEGER
    unique: true
Photo = sequelize.define 'photo',
  location: Sequelize.STRING
  itemId: 
    type: Sequelize.INTEGER
Photo.belongsTo User,
  foreignKey: 'itemId'
  targetKey: 'uid'

sequelize.sync()

The query generated by sync() ignores the targetKey configuration:

CREATE TABLE IF NOT EXISTS "photos" (
    "id"   SERIAL , 
    "location" VARCHAR(255), 
    "itemId" INTEGER REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, 
    "updatedAt" TIMESTAMP WITH TIME ZONE NOT 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.

Activity

  1. guigrpa commented on Sep 8, 2015

    @guigrpa
    Author

    See also #4269 and #4258

  2. guigrpa commented on Sep 8, 2015

    @guigrpa
    Author

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

  3. mickhansen commented on Sep 8, 2015

    @mickhansen
    Contributor

    We have a working test here:

    it('should support a belongsTo with the targetKey option', function() {

  4. added
    type: bug
    on Sep 8, 2015
  5. guigrpa commented on Sep 8, 2015

    @guigrpa
    Author

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

    CREATE TABLE IF NOT EXISTS "photos" (
        "id"   SERIAL , 
        "location" VARCHAR(255), 
        "itemId" INTEGER, 
        "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, 
        "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, 
        PRIMARY KEY ("id")
    );

    ...and getting the User from a created Photo works correctly, just as in the test.

  6. mickhansen commented on Sep 8, 2015

    @mickhansen
    Contributor

    Hmm, curious if it works without removing the primary key, maybe it doesn't.

  7. jayfresh commented on Sep 15, 2015

    @jayfresh

    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.

  8. mickhansen commented on Sep 15, 2015

    @mickhansen
    Contributor

    Looks like the test was written like that because SQLITE does not support constraints on anything other than primary key.

  9. mickhansen commented on Sep 15, 2015

    @mickhansen
    Contributor

    Ah well, neither does postgres, looks like there shouldn't be a constraint at all then (unless it's a unique).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions