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

Mysql auto_increment_increment setting is not work. #5460

Closed
5 tasks done
pius712 opened this issue Apr 17, 2024 · 1 comment
Closed
5 tasks done

Mysql auto_increment_increment setting is not work. #5460

pius712 opened this issue Apr 17, 2024 · 1 comment

Comments

@pius712
Copy link

pius712 commented Apr 17, 2024

Describe the bug

If you set mysql variable auto_increment_increment to 2,
When saving multiple entities, pk should be incremented by 2.
However, if you use the following code in the MySqlDriver file, it will be set to 1.

async getAutoIncrementIncrement(ctx) {
        if (this.autoIncrementIncrement == null) {
            // the increment step may differ when running a cluster, see https://github.com/mikro-orm/mikro-orm/issues/3828
            const res = await this.connection.execute(`show variables like 'auto_increment_increment'`, [], 'get', ctx, { enabled: false });
            /* istanbul ignore next */
            this.autoIncrementIncrement = res?.auto_increment_increment ? +res?.auto_increment_increment : 1;
        }
        */ return this.autoIncrementIncrement;
    }

when I debugging, the res value is like below.

{
  "Variable_name": "auto_increment_increment",
  "Value": "2"
}

I want to pull request this, but I don't know why this code is written. So I report this on issue.

Reproduction

version Info:

  • mysql: 8.0.33
  • @mikro-orm/core": "^6.2.1
  • @mikro-orm/mysql": "^6.2.1
  • @mikro-orm/nestjs": "^5.2.3
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';

export abstract class BaseEntity {
  @PrimaryKey({ type: 'bigint', autoincrement: true })
  id?: string;

  @Property({ type: 'timestamp',  onCreate: () => new Date() })
  createTime: Date = new Date();

  @Property({
    type: 'timestamp',
    onUpdate: () => new Date(),
    nullable: true,
  })
  updateTime?: Date = new Date();
}

@Entity({tableName: 'post'})
export class PostEntity extends BaseEntity {

    @Property({type:'varchar'})
    title:string
    
    constructor(title: string) {
        super();
        this.title = title;
    }
}
const result = await this.postRepository.save([
      new PostEntity('title'),
      new PostEntity('title2'),
      new PostEntity('title3'),
    ]);
    return result;
  }

@Injectable()
export class PostRepository {
  constructor(
    @InjectRepository(PostEntity)
    private readonly postRepository: EntityRepository<PostEntity>,
  ) {}

  async save(entity: PostEntity | PostEntity[]): Promise<PostEntity | PostEntity[]> {
    await this.postRepository.getEntityManager().persistAndFlush(entity);

    return entity;
  }
}

What driver are you using?

@mikro-orm/mysql

MikroORM version

6.2.1

Node.js version

v18.16.0

Operating system

mac os

Validations

@B4nan
Copy link
Member

B4nan commented Apr 17, 2024

Hmm weird, even looking at the original issue, the screenshot there confirms the same format as you say (and I can see it too, both in MySQL 8 and mariadb - will check MySQL 5 to be sure its not about that, but I kinda doubt it). I guess I must have been hallucinating, lol.

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