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

Fields with the same name as the table receive primary key contents #1349

Closed
oliversalzburg opened this issue Jan 26, 2021 · 1 comment
Closed
Assignees

Comments

@oliversalzburg
Copy link
Contributor

oliversalzburg commented Jan 26, 2021

Describe the bug
When a property has the same field name as the table, then that field will be filled with the primary key of the entity when loaded.

To Reproduce
This test should show the issue:

import { AbstractNamingStrategy, Collection, Entity, EntityRepository, IdentifiedReference, Logger, ManyToMany, ManyToOne, MikroORM, OneToMany, OneToOne, PrimaryKey, PrimaryKeyProp, PrimaryKeyType, Property, Reference } from '@mikro-orm/core';
import { AbstractSqlDriver, SchemaGenerator } from '@mikro-orm/knex';

class TestNamingStrategy extends AbstractNamingStrategy {

  classToTableName(entityName: string): string {
    throw new Error('Method not implemented.');
  }

  propertyToColumnName(propertyName: string): string {
    return propertyName;
  }

  referenceColumnName(): string {
    throw new Error('Method not implemented.');
  }

  joinColumnName(propertyName: string): string {
    return propertyName;
  }

  joinTableName(
    sourceEntity: string,
    targetEntity: string,
    propertyName?: string | undefined
  ): string {
    throw new Error('Method not implemented.');
  }

  joinKeyColumnName(entityName: string, referencedColumnName?: string | undefined): string {
    return entityName.toLocaleLowerCase();
  }

}

@Entity({ tableName: 'name' })
class Name {

  @PrimaryKey()
  id!: number;

  @Property({ fieldName: 'name' })
  realName!: string;

  @ManyToMany({ entity: 'User', pivotTable: 'userNames', mappedBy: 'names' })
  users = new Collection<User>(this);

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

}

@Entity({ tableName: 'user' })
class User {

  @PrimaryKey()
  id!: number;

  @ManyToMany({ entity: 'Name', pivotTable: 'userNames' })
  names = new Collection<Name>(this);

}

describe('preserve data fields', () => {

  let orm: MikroORM<AbstractSqlDriver>;
  const log = jest.fn();

  beforeAll(async () => {
    orm = await MikroORM.init({
      entities: [User, Name],
      dbName: `mikro_orm_test_pivot_fields_wa`,
      type: 'postgresql',
      cache: { enabled: false },
      namingStrategy: TestNamingStrategy,
    });

    await new SchemaGenerator(orm.em).ensureDatabase();
  });


  beforeEach(async () => {
    await orm.getSchemaGenerator().dropSchema();
    await orm.getSchemaGenerator().createSchema();
  });

  afterAll(() => orm.close(true));

  test('preserves data fields with same name as table', async () => {
    const user = orm.em.create<User>(User, {});
    const name = orm.em.create<Name>(Name, { id: 42, realName: 'this is my name' });
    user.names.add(name);
    await orm.em.persistAndFlush([user, name]);

    // Ensure we don't get the data from the existing identity map.
    orm.em.clear();

    const entity = await orm.em.findOneOrFail<User>(User, user, { populate: ['names'], refresh: true });
    expect(entity.names[0].realName).toEqual('this is my name');
  });

});

Expected behavior
Fields should always receive the data from the correct column.

Versions

Dependency Version
node 14
typescript 4
mikro-orm HEAD
your-driver postgresql
@B4nan
Copy link
Member

B4nan commented Jan 26, 2021

That test is passing for me locally.

@B4nan B4nan closed this as completed in 56682be Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants