From 06ac26e9e422a51a956e78e5b4ae58305c2ffdf0 Mon Sep 17 00:00:00 2001 From: Neo Ryo Date: Mon, 22 Apr 2024 09:23:23 +0200 Subject: [PATCH] fixed migration is workin --- server/controllers/countries.ts | 4 +++- server/entities/country.ts | 7 +------ server/entities/user.ts | 2 +- .../1713446226165-user-village-country-relation.ts | 7 +++---- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/server/controllers/countries.ts b/server/controllers/countries.ts index 0b2ef958a..452662c6e 100644 --- a/server/controllers/countries.ts +++ b/server/controllers/countries.ts @@ -1,11 +1,13 @@ import { UserType } from '../../types/user.type'; -import { countries } from '../utils/iso-3166-countries-french'; +import { Country } from '../entities/country'; +import { AppDataSource } from '../utils/data-source'; import { Controller } from './controller'; const countryController = new Controller('/countries'); //--- Get all countries --- countryController.get({ path: '', userType: UserType.TEACHER }, async (_req, res) => { + const countries = await AppDataSource.getRepository(Country).find(); res.sendJSON(countries); }); diff --git a/server/entities/country.ts b/server/entities/country.ts index 563365bad..57b12e498 100644 --- a/server/entities/country.ts +++ b/server/entities/country.ts @@ -1,6 +1,4 @@ -import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; - -import { User } from './user'; +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Country { @@ -12,7 +10,4 @@ export class Country { @Column({ type: 'text', nullable: false }) public name: string; - - @ManyToOne(() => User, (user: User) => user) - users: User[]; } diff --git a/server/entities/user.ts b/server/entities/user.ts index c0eed74e7..b0c9e8f2c 100644 --- a/server/entities/user.ts +++ b/server/entities/user.ts @@ -78,7 +78,7 @@ export class User { @Column({ nullable: true }) public villageId: number | null; - @OneToMany(() => Country, (country: Country) => country) + @ManyToOne(() => Country, (country: Country) => country) public country: Country | null; @Column({ type: 'decimal', precision: 11, scale: 8, nullable: false, default: 0 }) diff --git a/server/migrations/1713446226165-user-village-country-relation.ts b/server/migrations/1713446226165-user-village-country-relation.ts index 34488248f..d76fc1be9 100644 --- a/server/migrations/1713446226165-user-village-country-relation.ts +++ b/server/migrations/1713446226165-user-village-country-relation.ts @@ -32,11 +32,10 @@ export class UserVillageCountryRelation1713446226165 implements MigrationInterfa } } // user country relation - const usersCountry: { id: number; countryCode: string }[] = await queryRunner.query(`SELECT id, countryCode FROM user;`); - - await queryRunner.query(`ALTER TABLE user + await queryRunner.query(`ALTER TABLE user ADD COLUMN countryId int, - ADD CONSTRAINT FK_6f937fd92e219e3e5fa70aea9c7 FOREIGN KEY (countryId) REFERENCES country(id) ON DELETE NO ACTION ON UPDATE NO ACTION`); + ADD CONSTRAINT FK_4aaf6d02199282eb8d3931bff31 FOREIGN KEY (countryId) REFERENCES country(id) ON DELETE NO ACTION ON UPDATE NO ACTION`); + const usersCountry: { id: number; countryCode: string }[] = await queryRunner.query(`SELECT id, countryCode FROM user;`); // save data stored for (const userCountry of usersCountry) { const countryId: { id: number }[] = await queryRunner.query(`SELECT id FROM country WHERE isoCode='${userCountry.countryCode}';`);