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 signed primary key migration #5485

Closed
5 tasks done
HermanFassett opened this issue Apr 21, 2024 · 3 comments
Closed
5 tasks done

MySql signed primary key migration #5485

HermanFassett opened this issue Apr 21, 2024 · 3 comments

Comments

@HermanFassett
Copy link

Describe the bug

I have an entity that has a signed primary key like so:

@PrimaryKey({ autoincrement: false, type: new BigIntType('bigint'), unsigned: false })`
id!: number;

I am trying to use in another class as a ManyToOne reference, but in the generated migrations, the generated foreign key user_id is of type unsigned so running the migration fails with error Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'message_user_id_foreign' are incompatible (or similar error, in repro the error is a missing index). Am I missing something that can get this working?

Reproduction

https://github.com/HermanFassett/mikro-orm-repro/

What driver are you using?

@mikro-orm/mysql

MikroORM version

6.2.2

Node.js version

20.12.0

Operating system

Windows 10

Validations

@B4nan
Copy link
Member

B4nan commented Apr 21, 2024

@PrimaryKey({ autoincrement: false, type: new BigIntType('bigint'), unsigned: false })`
id!: number;

Just FYI this definition does not make much sense, you use new BigIntType('bigint'), but map to a JS number? Sounds like you want this instead:

@PrimaryKey({ autoincrement: false, unsigned: false })`
id!: bigint;

(it won't fix the problem you are reporting, but at least you won't be lying to yourself - the ORM would hydrate bigints there, not numbers.

@HermanFassett
Copy link
Author

Just FYI this definition does not make much sense, you use new BigIntType('bigint'), but map to a JS number?

Yes I believe I originally did that since I do want a JS number, but MySQL int does not support same max/min value as JS number so I was getting errors saving some values to the database.

@B4nan B4nan closed this as completed in b7ae145 May 5, 2024
@B4nan
Copy link
Member

B4nan commented May 5, 2024

Yes I believe I originally did that since I do want a JS number, but MySQL int does not support same max/min value as JS number so I was getting errors saving some values to the database.

My point is that new BigIntType('bigint') means "bigint column that maps to bigint JS type". If you want to map to JS number, you need new BigIntType('number'), if you want to map to JS bigint, you should declare the property type as bigint (and in such case, you don't need to explicitly use the BigIntType as mentioned in the previous comment).

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