Stack Exchange
Why Typeorm does always fail when trying to insert to 2 tables and Math.random() returns always same number?
So i have following code:
import { getRepository } from "typeorm";
import { NextFunction, Request, Response } from "express";
import { Users } from "../entity/Users";
import { Verify } from "../entity/Verify";
import { VerifyController } from "./VerifyController";

export class UserController {

    private userRepository = getRepository(Users);
    private verifyRepository = getRepository(Verify);

    // async all(request: Request, response: Response, next: NextFunction) {
    //     return this.userRepository.find();
    // }

    // async one(request: Request, response: Response, next: NextFunction) {
    //     var user = await this.userRepository.findOne(request.params.id);
    //     if(user) {
    //         return user;
    //     }
    //     return {Error: "Couldn't find user"};
    // }

    // async save(request: Request, response: Response, next: NextFunction) {
    //     return this.userRepository.save(request.body);
    // }

    // async remove(request: Request, response: Response, next: NextFunction) {
    //     let userToRemove = await this.userRepository.findOne(request.params.id);
    //     await this.userRepository.remove(userToRemove);
    // }

    async register(request: Request, response: Response, next: NextFunction) {
        var body = request.body;
        var isUsername = await this.userRepository.count({username: body.username});
        if(isUsername > 0) {
            return {Status: "ERROR", ERR: "Username already exists!"}
        }
        var isEmail = await this.userRepository.count({email: body.email});
        if(isEmail > 0) {
            return {Status: "ERROR", ERR: "Email already exists!"}
        }
        var loop = true;
        var UserID = this.generate(10);
        while(loop) {
            var isUserID = await this.userRepository.count({userID: UserID});
            if(isUserID > 0) {
                UserID = this.generate(10);
            } else {
                loop = false;
            }
        }
        body.userID = UserID;
        this.userRepository.save(body);
        var tomorrow = new Date();
        tomorrow.setDate(tomorrow.getDate() + 1);
        var verifyID = this.generate(20);
        loop = true;
        while(loop) {
            var verifyCount = await this.verifyRepository.count({verify_id: verifyID});
            if(verifyCount > 0) {
                loop = false;
            } else {
                verifyCount = this.generate(20);
            }
        }
        this.verifyRepository.save({account_id: body.userID, verify_id: verifyID, validUntil: tomorrow });
        return {Status: "OK", UserID: body.userID};
    }

    async nameCheck(request: Request, response: Response, next: NextFunction) {
        var isUsername = await this.userRepository.count({username: request.body.name});
        if(isUsername > 0) {
            return {Status: "ERROR"};
        }
        return {Status: "OK"};
    }

    generate(n) {
        var rN = "";
        while(n > 0) {
            rN += Math.floor(Math.random() * 10).toString();     // returns a random integer from 0 to 9
            n--;  
        }
        return parseInt(rN);
    }


}

This is a function inside a TypeORM Controller. I've started with the default express template from TypeORM and added this function. I commented the other functions out because i dont use the default ones.
Anyway the function that doesn't seem to work as intended is register.
The problems i have with this function are the following:

I try to insert into two tables but it just inserts into the first one named "users" and the second one seems to be empty even after multiple atempts.
I use the function "generate" to genrate random user ids of length 10 and 20. But every time i restart my typeorm server and drop the database, the id is the same. Every time. The function always returns "2147483647" but when i try to debug it with console.log() i get a real random number. Also when trying to make a second number with length 20 it returns the same number 2147483647. And i dont know why

I realy hope someone could help me.
For better understanding i will also post the entity classes:
Users.ts:
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class Users {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    username: string;

    @Column()
    userID: number

    @Column()
    email: string;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    birthday: Date;

    @Column()
    ipAdress: string;

    @Column()
    verified: boolean;
}

Verify.ts:
import { Col } from "react-bootstrap";
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class Verify {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    verify_id: number;

    @Column()
    account_id: number;

    @Column()
    validUntil: Date;
}


Fixed it myself.
I did find the solution by myself and not on the internet.
The problem is that by default the int is set in mysql to int(10) which shorts my digit to the described one.
I tried to use bigint to solve this problem and it worked at least with the 10 digit number.
But it looks like you can't store 20 digit numbers in the database as actual int.
It is possible with Strings i think.
