Skip to content

hasOne 'as' option produces wrong foreign key #10972

@demosglok

Description

@demosglok

What are you doing?

i'm adding hasOne relationship with {as: ''} option, which leads to foreignKey in target table to be derived from alias name. Though same example is in docs, that is not what i'm expecting

models.User.hasOne(models.Device, {as: 'Calculator'})

which leads to query SELECT .. LEFT OUTEER JOIN Devices As Calculator ON User.id = Calculator.CalculatorId instead of Calculator.userId

To Reproduce
Steps to reproduce the behavior:

  1. Define models X, Y
  2. Set association 'hasOne' with alias: X.hasOne(Y, {as: 'Z'})
  3. See error in query, Z.ZId instead of Z.XId

What do you expect to happen?

I want foreign key on target table to be left untouched and derived from source table

What is actually happening?

foreign key is derived from alias instead of source table name

Environment

Dialect:

  • mysql
  • postgres
  • [*] sqlite
  • mssql
  • any
    Sequelize version: 5.7.3
    Node Version: 10.x
    OS: windows 10

Metadata

Metadata

Assignees

Labels

docsFor issues and PRs. Things related to documentation, such as changes in the manuals / API reference.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions