Skip to content

Commit

Permalink
fixed migration is workin
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ryo committed Apr 22, 2024
1 parent a848027 commit 06ac26e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
4 changes: 3 additions & 1 deletion server/controllers/countries.ts
Original file line number Diff line number Diff line change
@@ -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);
});

Expand Down
7 changes: 1 addition & 6 deletions server/entities/country.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,7 +10,4 @@ export class Country {

@Column({ type: 'text', nullable: false })
public name: string;

@ManyToOne(() => User, (user: User) => user)
users: User[];
}
2 changes: 1 addition & 1 deletion server/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}';`);
Expand Down

0 comments on commit 06ac26e

Please sign in to comment.