-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecb948b
commit 2fa6e17
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
MigrationInterface, | ||
QueryRunner, | ||
Table, | ||
TableForeignKey, | ||
} from 'typeorm'; | ||
|
||
export class createAddress1637865030266 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.createTable( | ||
new Table({ | ||
name: 'address', | ||
columns: [ | ||
{ | ||
name: 'id', | ||
type: 'int', | ||
isPrimary: true, | ||
}, | ||
{ | ||
name: 'complement', | ||
type: 'varchar', | ||
}, | ||
{ | ||
name: 'zipcode', | ||
type: 'varchar', | ||
}, | ||
{ | ||
name: 'neighborhood', | ||
type: 'varchar', | ||
}, | ||
{ | ||
name: 'public_place', | ||
type: 'varchar', | ||
}, | ||
{ | ||
name: 'city_id', | ||
type: 'int', | ||
}, | ||
{ | ||
name: 'user_id', | ||
type: 'int', | ||
}, | ||
], | ||
}), | ||
true | ||
); | ||
|
||
await queryRunner.createForeignKey( | ||
'address', | ||
new TableForeignKey({ | ||
columnNames: ['city_id'], | ||
referencedColumnNames: ['id'], | ||
referencedTableName: 'city', | ||
onDelete: 'CASCADE', | ||
}) | ||
); | ||
|
||
await queryRunner.createForeignKey( | ||
'address', | ||
new TableForeignKey({ | ||
columnNames: ['user_id'], | ||
referencedColumnNames: ['id'], | ||
referencedTableName: 'user', | ||
onDelete: 'CASCADE', | ||
}) | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
const table = await queryRunner.getTable('address'); | ||
|
||
const userId = table.foreignKeys.find( | ||
(fk) => fk.columnNames.indexOf('user_id') !== -1 | ||
); | ||
await queryRunner.dropForeignKey('address', userId); | ||
|
||
const cityId = table.foreignKeys.find( | ||
(fk) => fk.columnNames.indexOf('city_id') !== -1 | ||
); | ||
await queryRunner.dropForeignKey('address', cityId); | ||
|
||
await queryRunner.dropTable('address'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; | ||
import { City } from './city.schema'; | ||
import { User } from './user.schema'; | ||
|
||
@Entity() | ||
export class Address { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
@Column() | ||
complement: string; | ||
|
||
@Column() | ||
zipcode: string; | ||
|
||
@Column() | ||
neighborhood: string; | ||
|
||
@Column() | ||
public_place: string; | ||
|
||
@Column() | ||
city_id: number; | ||
|
||
@Column() | ||
user_id: number; | ||
|
||
@ManyToOne(() => User, (user) => user.addresses) | ||
user: User; | ||
|
||
@ManyToOne(() => City, (city) => city.addresses) | ||
city: City; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters