Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embeddables: fieldName option is ignored #2165

Closed
edorgeville opened this issue Aug 30, 2021 · 4 comments
Closed

Embeddables: fieldName option is ignored #2165

edorgeville opened this issue Aug 30, 2021 · 4 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@edorgeville
Copy link

edorgeville commented Aug 30, 2021

Describe the bug
Now that #1948 is implemented and merged (thank you so much 🙌) I am trying to migrate some code that was previously using ObjectIds to embeddables + m:1.
It seems that the fieldName parameter in ManyToOne is ignored.

To Reproduce

Code
import { Embeddable, Embedded, Entity, ManyToOne, MikroORM, PrimaryKey, Property } from '@mikro-orm/core';
import type { SqliteDriver } from '@mikro-orm/sqlite';

@Entity()
export class User {

  @PrimaryKey()
  id!: number;

  @Property()
  name: string = '';

  constructor(name: string) {
    this.name = name;
  }

}

@Embeddable()
export class FamilyMember {

  @Property()
  relation: string = 'brother';

  @ManyToOne(() => User, { eager: true, fieldName: 'user_id' }) // we want to save our relationship inside a `user_id` field
  user!: User;

}

@Entity()
export class Family {

  @PrimaryKey()
  id!: number;

  @Embedded(() => FamilyMember, { array: true })
  members: FamilyMember[] = [];

}

describe('GH issue 1003', () => {

  let orm: MikroORM<SqliteDriver>;

  beforeAll(async () => {
    orm = await MikroORM.init({
      entities: [FamilyMember, User, Family],
      dbName: ':memory:',
      type: 'sqlite',
    });
    await orm.getSchemaGenerator().createSchema();
  });

  afterAll(async () => {
    await orm.close(true);
  });

  test(`GH issue 2165`, async () => {
    const family = new Family();

    const dad = new FamilyMember();
    dad.relation = 'dad';
    dad.user = new User('John');
    family.members.push(dad);

    const mom = new FamilyMember();
    mom.relation = 'mom';
    mom.user = new User('Jane');
    family.members.push(mom);

    console.log(family);

    await orm.em.persistAndFlush(family);

    const nativeResults: Family[] = await orm.em.createQueryBuilder(Family).execute();

    // Relationship is saved in `user` field instead of `user_id`
    console.log(nativeResults[0].members);
  });

});

Also available in repro-repo here: https://github.com/edorgeville/mikro-orm-embeddable-m1-fieldName

Expected behavior
Expected relationships to be saved according to fieldName property. Instead, it uses the class property name.

Versions

Dependency Version
node v14.17.3
typescript v10.0.0
@mikro-orm/core ^5.0.0-dev.250
@mikro-orm/mongodb ^5.0.0-dev.250
@B4nan B4nan added the bug Something isn't working label Aug 31, 2021
@B4nan B4nan added this to the 5.0 milestone Aug 31, 2021
@B4nan
Copy link
Member

B4nan commented Oct 9, 2021

I've been struggling with fixing this for quite some time unfortunately, almost got it fixed but it would break em.create() as it is also using hydrator :/

Need to move to other things now as I can't stall v5 release for one bug. Will try to get back to it later.

@edorgeville
Copy link
Author

Not a showstopper for me, our current ObjectId workaround will do in the meantime. Thanks for the update, good luck for 5.0 release 🙏

@titivuk
Copy link

titivuk commented Nov 15, 2022

Hello
Any updates on this?

@B4nan B4nan modified the milestones: 5.0, 6.0 Feb 17, 2023
B4nan added a commit that referenced this issue Oct 22, 2023
B4nan added a commit that referenced this issue Oct 22, 2023
B4nan added a commit that referenced this issue Oct 23, 2023
…ded properties

This is breaking mainly for SQL drivers, where the default naming strategy is underscoring, and will now applied to the embedded properties too. You can restore to the old behaviour by implementing custom naming strategy, overriding the `propertyToColumnName` method. It now has a second boolean parameter to indicate if the property is defined inside a JSON object context.

```ts
import { UnderscoreNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends UnderscoreNamingStrategy {

   propertyToColumnName(propertyName: string, object?: boolean): string {
      if (object) {
         return propertyName;
      }

      return super.propertyToColumnName(propertyName, object);
   }

}
```

Closes #4371
Closes #2165
Closes #2361
B4nan added a commit that referenced this issue Oct 23, 2023
…ded properties (#4866)

This is breaking mainly for SQL drivers, where the default naming
strategy is underscoring, and will now applied to the embedded
properties too. You can restore to the old behaviour by implementing
custom naming strategy, overriding the `propertyToColumnName` method. It
now has a second boolean parameter to indicate if the property is
defined inside a JSON object context.

```ts
import { UnderscoreNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends UnderscoreNamingStrategy {

   propertyToColumnName(propertyName: string, object?: boolean): string {
      if (object) {
         return propertyName;
      }

      return super.propertyToColumnName(propertyName, object);
   }

}
```

Closes #4371
Closes #2165
Closes #2361
@B4nan
Copy link
Member

B4nan commented Oct 23, 2023

Closing as fixed in v6 via #4866

@B4nan B4nan closed this as completed Oct 23, 2023
B4nan added a commit that referenced this issue Oct 23, 2023
…ded properties

This is breaking mainly for SQL drivers, where the default naming strategy is underscoring, and will now applied to the embedded properties too. You can restore to the old behaviour by implementing custom naming strategy, overriding the `propertyToColumnName` method. It now has a second boolean parameter to indicate if the property is defined inside a JSON object context.

```ts
import { UnderscoreNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends UnderscoreNamingStrategy {

   propertyToColumnName(propertyName: string, object?: boolean): string {
      if (object) {
         return propertyName;
      }

      return super.propertyToColumnName(propertyName, object);
   }

}
```

Closes #4371
Closes #2165
Closes #2361
B4nan added a commit that referenced this issue Oct 25, 2023
…ded properties (#4866)

This is breaking mainly for SQL drivers, where the default naming
strategy is underscoring, and will now applied to the embedded
properties too. You can restore to the old behaviour by implementing
custom naming strategy, overriding the `propertyToColumnName` method. It
now has a second boolean parameter to indicate if the property is
defined inside a JSON object context.

```ts
import { UnderscoreNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends UnderscoreNamingStrategy {

   propertyToColumnName(propertyName: string, object?: boolean): string {
      if (object) {
         return propertyName;
      }

      return super.propertyToColumnName(propertyName, object);
   }

}
```

Closes #4371
Closes #2165
Closes #2361
B4nan added a commit that referenced this issue Nov 2, 2023
…ded properties (#4866)

This is breaking mainly for SQL drivers, where the default naming
strategy is underscoring, and will now applied to the embedded
properties too. You can restore to the old behaviour by implementing
custom naming strategy, overriding the `propertyToColumnName` method. It
now has a second boolean parameter to indicate if the property is
defined inside a JSON object context.

```ts
import { UnderscoreNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends UnderscoreNamingStrategy {

   propertyToColumnName(propertyName: string, object?: boolean): string {
      if (object) {
         return propertyName;
      }

      return super.propertyToColumnName(propertyName, object);
   }

}
```

Closes #4371
Closes #2165
Closes #2361
B4nan added a commit that referenced this issue Nov 5, 2023
…ded properties (#4866)

This is breaking mainly for SQL drivers, where the default naming
strategy is underscoring, and will now applied to the embedded
properties too. You can restore to the old behaviour by implementing
custom naming strategy, overriding the `propertyToColumnName` method. It
now has a second boolean parameter to indicate if the property is
defined inside a JSON object context.

```ts
import { UnderscoreNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends UnderscoreNamingStrategy {

   propertyToColumnName(propertyName: string, object?: boolean): string {
      if (object) {
         return propertyName;
      }

      return super.propertyToColumnName(propertyName, object);
   }

}
```

Closes #4371
Closes #2165
Closes #2361
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants