Is there any ways to clear two tables connected with ManyToOne?
Table A
#Entity('table_A')
export class TableA{
#PrimaryGeneratedColumn()
id: number;
#Column()
B_ID: number;
#ManyToOne(() => TableB)
tableB: TableB;
#Column()
userId: number;
#ManyToOne(() => User)
user: User;
}
Table B
#Entity('table_B')
export class TableB{
#PrimaryGeneratedColumn()
id: number;
#Column({ name: 'time', type: 'text' })
time: Date;
#Column({ name: 'msgid', type: 'int', nullable: true })
msgid: number;
#Column({ name: 'event', type: 'text', nullable: true })
event: string;
}
I can use this.tableARepository.clear(); to clear Table_A but when I run this.tableBRepository.clear(); Data in Table A restores.
Anyone have a clue on this? Any help is much appreciated.
Related
I have two entities, users and roleuser, I have one to many mapping from user to role and many to one on user. while I am querying the result set, I am getting error as cannot read property joincolumns of undefined.Could anyone help me in solving this error, what am I doing wrong
Here are my entities.
// mosaique_user
#Entity()
export class MosaiqueUser {
#PrimaryGeneratedColumn()
id: number;
#Column({nullable: true, length: 100})
name: string;
#Column({unique: true, length: 255})
email: string;
#Column({nullable: true})
password: string;
#Column({nullable: true})
is_temp: number;
#Column({nullable: true})
salt: string;
#Column({nullable: true, default: 2})
status: number;
#Column({nullable: true, default: 2})
apply_status: number;
#Column({ type: 'timestamp', nullable: true})
created_at: Date;
#Column({ type: 'timestamp', nullable: true})
updated_at: Date;
#Column({ nullable: true})
created_by: number;
#Column({ nullable: true})
updated_by: number;
#OneToOne(() => MosaiqueUserProfile, userprofile => userprofile.user)
userprofile: MosaiqueUserProfile;
#OneToMany(() => RoleUser, userRole => userRole.user_id, {
cascade: true,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
#JoinColumn({ referencedColumnName: 'user_id' })
userRoles!: RoleUser[];
}
// role_user
#Entity()
export class RoleUser {
#PrimaryGeneratedColumn()
id: number;
#Column()
user_id: number;
#Column()
role_id: number;
#CreateDateColumn()
created_at: Date;
#UpdateDateColumn()
updated_at: Date;
#ManyToOne(() => MosaiqueUser, user => user.userRoles)
#JoinColumn({ name: 'user_id' })
user!: MosaiqueUser;
#ManyToOne(() => MRole, role => role.userRoles)
#JoinColumn({ name: 'role_id' })
role!: MRole;
}
// with relations query
let userData = await this.mosaiqueUser.find({
relations: ['userRoles'],
where: {'id':1}
});
Error
(node:57872) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'joinColumns' of undefined
at C:\Sites\Dynamodb\TypeORMProject\src\query-builder\SelectQueryBuilder.ts:1549:61
at Array.map ()
at SelectQueryBuilder.createJoinExpression (C:\Sites\Dynamodb\TypeORMProject\src\query-builder\SelectQueryBuilder.ts:1520:57)
at SelectQueryBuilder.getQuery (C:\Sites\Dynamodb\TypeORMProject\src\query-builder\SelectQueryBuilder.ts:57:21)
at SelectQueryBuilder.QueryBuilder.getQueryAndParameters (C:\Sites\Dynamodb\TypeORMProject\src\query-builder\QueryBuilder.ts:446:28)
at SelectQueryBuilder. (C:\Sites\Dynamodb\TypeORMProject\src\query-builder\SelectQueryBuilder.ts:2112:40)
at step (C:\Sites\Dynamodb\TypeORMProject\node_modules\tslib\tslib.js:144:27)
at Object.next (C:\Sites\Dynamodb\TypeORMProject\node_modules\tslib\tslib.js:125:57)
at C:\Sites\Dynamodb\TypeORMProject\node_modules\tslib\tslib.js:118:75
at new Promise ()
I am trying to fetch data using Typeform in my NestJs project.
I am trying to join three tables.
User table
Roles and user forginkey table (connect between tables 1 & 3)
Roles table
My user table entity
#Entity()
export class Users {
#PrimaryGeneratedColumn()
id: number;
#Column()
email: string;
#Column()
passwordHash: string;
#Column()
firstName: string;
#Column()
lastName: string;
#Column()
registerTimestamp: Date;
#Column({ default: false })
isActive: boolean;
#OneToOne(() => UsersRole)
#JoinColumn({ name: 'id' })
users_role: UsersRole;
}
My Users_Roles code
#Entity()
export class UsersRole {
#PrimaryColumn()
user_id: number;
#PrimaryColumn()
role_id: number;
#OneToOne(() => UsersRole)
#JoinColumn({ name: 'role_id' })
roles: UsersRole;
}
My Roles table code
#Entity()
export class UsersRole {
#PrimaryColumn()
id: number;
#PrimaryColumn()
role: string;
}
How do I join between them three tables using Typeform?
I created an API which is returning selected fields of user data.
User model:
#Exclude()
#Entity('users')
export class User extends Timestampable {
#ApiProperty()
#PrimaryGeneratedColumn()
id: number;
#ApiProperty()
#Expose({ groups: ['create', 'profile'] })
#Column({ unique: true })
email: string;
#ApiProperty()
#Expose({ groups: ['create'] })
#Column()
password: string;
#ApiProperty()
#Expose({ groups: ['profile'] })
#Column({ nullable: true })
firstName: string;
#ApiProperty()
#Expose({ groups: ['profile'] })
#Column({ nullable: true })
lastName: string;
#ApiProperty()
#Expose({ groups: ['profile'] })
#Column({ nullable: true })
paternalName: string;
#ApiProperty()
#Expose({ groups: ['profile'] })
#Column({ nullable: true })
phone: string;
#ApiProperty()
#Expose({ groups: ['profile'] })
#Column({ nullable: true, type: 'enum', enum: UserGender })
gender: UserGender;
}
Inside the controller, I just return:
return instanceToPlain(user, {
groups: ['profile'],
});
here will be returned only the fields of the user which have a profile group.
Everything working properly. But for swagger documentation I used:
{
schema: {shows
$ref: getSchemaPath(User),
},
}
and in the documentation, it shows all fields of the user.
How can I show only fields from the particular group?
One of the options create 2 different DTOs and use them but that solution in my opinion is not that good.
I'm setting up TypeORM with a MySQL DB - here are 2 tables that should surely introduce foreign keys:
#Entity('Area')
export class Area {
#PrimaryGeneratedColumn('uuid')
Id: string;
#Column({
unique: true,
})
Name: string;
#CreateDateColumn()
CreatedAt: Date;
#UpdateDateColumn()
UpdatedAt: Date;
#OneToMany(() => Subarea, (Subarea) => Subarea.Area)
Subareas: Subarea[];
}
and
#Entity('Subarea')
export class Subarea {
#PrimaryGeneratedColumn('uuid')
Id: string;
#Column()
Name: string;
#CreateDateColumn()
CreatedAt: Date;
#UpdateDateColumn()
UpdatedAt: Date;
#ManyToOne(() => Area, (Area) => Area.Subareas)
#JoinColumn({ name: 'AreaId', referencedColumnName: 'Id' })
Area: Area;
}
Yet I get 2 totally separate, unrelated tables?
One of the devs helped me confirm it is a bug in current release, but has been fixed in master: https://github.com/typeorm/typeorm/pull/8038
I'm using CQRS and TypeORM in nestJs for create an user, orders and products application.
I have these entities in typeorm and when I run the command for create an user, I get an error that email is duplicated, when check the Database found out that there is 2 users with different id but one of them is empty. I don't know what's happening.
UserEntity:
#Entity()
export class UserEntity {
#PrimaryGeneratedColumn('uuid')
id!: string;
#Column({unique: true})
email!: string;
#Column()
password!: string;
#Column()
name!: string;
#Column()
role!: string;
#OneToMany(type => OrderEntity, order => order.userId)
orders?: OrderEntity[] | any;
#OneToMany(type => ItemEntity, item => item.userId)
items?: ItemEntity[] | any;
}
OrderEntity:
#Entity()
export class OrderEntity {
#PrimaryGeneratedColumn('uuid')
id!: string;
#Column()
buyerName!: string;
#Column()
buyerPhone!: number;
#Column()
buyerEmail!: string;
#Column()
shippingMethod!: string;
#Column()
shippingAddress!: string;
#Column()
shippingCity!: string;
#Column()
shippingRegion!: string;
#Column()
shippingCountry!: string;
#Column({ type: 'datetime'})
creationDate!: Date;
#Column({ type: 'datetime'})
pack_promise_min!: Date;
#Column({ type: 'datetime'})
pack_promise_max!: Date;
#Column({ type: 'datetime'})
ship_promise_min!: Date;
#Column({ type: 'datetime'})
ship_promise_max!: Date;
#Column({ type: 'datetime'})
delivery_promise_min!: Date;
#Column({ type: 'datetime'})
delivery_promise_max!: Date;
#Column({ type: 'datetime'})
ready_pickup_promise_min!: Date;
#Column({ type: 'datetime'})
ready_pickup_promise_max!: Date;
#Column()
status!: string;
#ManyToOne(type => UserEntity, user => user.orders)
userId: UserEntity | any;
#ManyToMany(type => ItemEntity, item => item.id, { cascade: true })
#JoinTable({ name: 'orders_items'})
items: ItemEntity[];
}
ItemEntity:
#Entity()
export class ItemEntity {
#PrimaryGeneratedColumn('uuid')
id!: string;
#Column()
name!: string;
#Column()
qty!: string;
#Column()
weight!: number;
#ManyToOne(type => UserEntity, user => user.items)
userId!: UserEntity | any;
#ManyToMany((type => OrderEntity), order => order.id, {cascade: true})
orders?: OrderEntity[]
}
All this is happening since I declared the relations, I think the problem is there.
This happened in the database:
the answer it's too stupid like me xD. I was making a console.log on the controller, just for see some of information, and the command execute itself two times. Sorry, my mistake.