Skip to content

repository.create strips values of columns that are both arrays and embedded objects #1980

Closed

Description

@skreborn

Issue type:

[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[x] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native

TypeORM version:

[x] latest
[x] @next
[ ] 0.x.x

Steps to reproduce or a small repository showing the problem:

When using repository.create, TypeORM strips array type column values before returning the object instance.

Given a simple Foo entity, which in turn contains a collection of Bar objects

@Entity()
export class Foo {
  @ObjectIdColumn()
  public id: ObjectID;

  @Column()
  public simple: string;

  @Column(type => Bar)
  public collection: Bar[];
}

export class Bar {
  @Column()
  value: boolean;
}

repository.create transforms the raw value

{
  "simple": "string",
  "collection": [
    { "value": true },
    { "value": false }
  ]
}

into simply

{ "simple": "string" }

Is this correct behavior?

NOTE: I am using create because the class in which I'm performing the operation is generic, and I have no way of simply creating the entity instances using their constructors.

Activity

  1. skreborn commented on Apr 20, 2018

    @skreborn
    Author

    The following value passes through without a problem, although I don't think that's correct behavior.

    {
      "simple": "string",
      "collection": { "value": true }
    }

    If I use my original object, I can see that ColumnMetadata.isArray correctly indicates that collection is supposed to be an array, but then ColumnMetadata.getEntityValue goes ahead and simply ignores this, finally reaching line 559, at which point it attempts to extract the value directly from the array [ { "value": true } ], which results in an undefined value.

    value = PromiseUtils.extractValue(embeddedObject[this.propertyName]);
  2. skreborn commented on Apr 20, 2018

    @skreborn
    Author

    I can also confirm that the issue isn't reproducible when the array is not of embedded objects, but simple types. Embedded objects, however, work correctly, given that they are not also arrays. The following setups both work flawlessly:

    @Entity()
    export class Foo {
      @ObjectIdColumn()
      public id: ObjectID;
    
      @Column()
      public simple: string;
    
      @Column()
      public collection: boolean[];
    }
    @Entity()
    export class Foo {
      @ObjectIdColumn()
      public id: ObjectID;
    
      @Column()
      public simple: string;
    
      @Column(type => Bar)
      public bar: Bar;
    }
    
    export class Bar {
      @Column()
      value: boolean;
    }

    Which means that the problem occurs only if a column is both an array and an embedded object.

  3. changed the title [-]TypeORM strips array type column values when using `repository.create`[/-] [+]`repository.create` strips values of columns that are both arrays and embedded objects[/+] on Apr 20, 2018
  4. self-assigned this
    on Apr 23, 2018
  5. thonythony commented on Jun 13, 2018

    @thonythony
    Contributor

    Hello,

    I have the same issue (my column is both an array and an embedded object).
    @skreborn, @pleerock have you a workaround using repository.create() ?

    Thanks

  6. mblackritter commented on Oct 1, 2019

    @mblackritter
  7. IbrahimAdDandan commented on Dec 16, 2021

    @IbrahimAdDandan

    and yes, Still behaving incorrectly :(

  8. luckv commented on Dec 23, 2021

    @luckv

    I also have this issue. Any plan on this?

  9. uzenith360 commented on May 22, 2022

    @uzenith360

    This is still a problem

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