Skip to content

Generated hasMany uses a FK column that does not exist #118

Description

@SikoNorge

A hasMany relation is generated with a snake_cased column derived from the parent's schema name, but the column the migration actually creates comes from the child's property name. For multi-word schema names the two never match.

ProductVariant.yaml:

properties:
  id: {type: integer, format: int64}
  packagings:
    type: array
    items:
      $ref: '#/components/schemas/ProductPackaging'

ProductPackaging.yaml:

properties:
  id: {type: integer, format: int64}
  productVariant:
    $ref: '#/components/schemas/ProductVariant'

The migration creates productVariant_id, and the belongs-to side is generated correctly:

// base/ProductPackaging.php
return $this->hasOne(ProductVariant::class, ['id' => 'productVariant_id']);

The has-many side is not:

// base/ProductVariant.php
return $this->hasMany(ProductPackaging::class, ['product_variant_id' => 'id']);

Using it throws:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product_variant_id' in 'WHERE'
SELECT * FROM `product_packaging` WHERE `product_variant_id`=1

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions