Can't see the ID of the hotel table in adminjs - node.js

I have 2 fields which are named hotel and hotel description. In admin page when I add the hotel ID to hotel description image 1. But in the hotel description table on the admin page, the id is not visible image 2.
Here is typeorm entities for them.
Hotel.entity
#Entity()
export class Hotel extends BaseEntity {
#PrimaryGeneratedColumn("uuid")
id: string;
#Column({ unique: true, nullable: true })
hotelName: string;
#OneToMany(
() => HotelDescription,
(hotelDescription) => hotelDescription.hotel,
)
hotelDescriptions: HotelDescription[];
#OneToMany(() => Reservation, (reservation) => reservation.hotel)
reservations: Reservation[];
}
HotelDescription.entity
#Entity()
export class HotelDescription extends BaseEntity {
#PrimaryGeneratedColumn("uuid")
id: string;
#Column({ type: "text", enum: Language, nullable: true })
language: Language;
#Column()
name: string;
#Column()
description: string;
#OneToOne(() => Service, (service) => service.hotelDescription)
services: Service;
#OneToMany(() => Treatment, (treatment) => treatment.hotelDescription)
treatments: Treatment[];
#ManyToOne(() => Hotel, (hotel) => hotel.hotelDescriptions)
hotel: Hotel;
}
I tried to #JoinColumn to try to add hotelId to the entity but that failed (removed in the code above)

Related

It is possible to do that OneToMany and ManyToMany between two entities or needs to be ManyToMany?

So a company can have multiple locations, that means OneToMany.
A Country can have multiple companies.
First I tried with ManyToOne in Country but I can have only 1 companyid per location.
How can I do that thing with the third table?
company.entity.ts
#Entity('company')
export class Company extends BaseEntity {
#PrimaryGeneratedColumn()
id: number
#IsString()
#Column({ type: 'varchar', nullable: true })
mission_statement: string
#IsDateString()
#Column({ type: 'date', nullable: true })
founded_in: Date
#OneToMany(() => Country, (country) => country.countryId, { eager: true })
locations: Country[]
}
country.entity.ts
#Entity('countries')
export class Country extends BaseEntity {
#PrimaryGeneratedColumn('uuid')
id: string
#IsNotEmpty()
#Column({ unique: true })
name: string
#ManyToMany(() => Company, (company) => company.locations, { nullable: true })
#JoinColumn({ name: 'company_id' })
countryId: Company[]
}
I know I should create a third entity for storing the relationship between them but I tried 4 times and for nothing. Do you think you can help me?

Cannot use has in the name of pivot table - TypeORM

#Entity('projects')
export class ProjectEntity extends BaseEntity {
#PrimaryGeneratedColumn()
id: number;
#ManyToMany(() => UserEntity, object => object.project, { cascade: true })
#JoinTable({name:'project_has_users'})
users: UserEntity[];
#Column()
name: string;
}
#Entity('users')
export class UserEntity {
#PrimaryGeneratedColumn()
id: number;
#ManyToMany(() => ProjectEntity, object => object.users)
project: ProjectEntity[];
#Column()
#IsEmail()
email: string;
}
QueryFailedError: null value in column "projectsId" of relation "project_has_users" violates not-null constraint

Trying to create a users membership table

I'm trying to create a membership table. This application has Accounts and Organizations and I initially just used a join table but then some new requirements came up such that the Organizational membership has a status, so I thought I'd create a new many to many table called OrganizationMembership. However when I did that it requires each OrganizationId to be unique my OrganizationMembership table, whereas I want a pair of AccountId and OrganizationId to be unique. I did try to read the help files for typeorm but I was confused as to how to. Any help would be appreciated, I included a trimmed down version of my 3 models below
#Entity()
export class OrganizationMembership {
#PrimaryGeneratedColumn()
id: number;
#Column()
status: string;
#Column()
role: string;
#OneToOne(type => Organization, organization => organization.memberships , {cascade: true})
#JoinColumn()
organization: Organization;
#OneToOne(type => Account, account => account.memberships , {cascade: true})
#JoinColumn()
member: Account;
}
#Entity()
export class Account {
#PrimaryGeneratedColumn()
id: number;
#Column()
email: string;
#Column({nullable: true})
password?: string;
#Column()
active: boolean;
#OneToMany(type => OrganizationMembership, organizationMembership => organizationMembership.member)
memberships: OrganizationMembership[];
}
#Entity()
export class Organization {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
#OneToMany(type => OrganizationMembership, organizationMembership => organizationMembership.member)
memberships: OrganizationMembership[];
}

Usage of ManyToOne relation returns null in TypeGraphQL

For learning, I made a project testing out TypeGraphql, together with TypeORM. I have a User and Book entity, where I want to have a created_by field on the Book entity.
#ObjectType()
#Entity()
export class Book extends BaseEntity {
#Field(() => ID)
#PrimaryGeneratedColumn()
readonly id: number;
#Field({ nullable: true })
#Column({ nullable: true })
name: string;
#Field(() => User)
#ManyToOne(() => User)
#JoinColumn({ name: 'created_by' })
created_by: User;
// #RelationId((orthodontist: Orthodontist) => orthodontist.created_by)
// createdById: number;
}
#ObjectType()
#Entity()
export class User extends BaseEntity {
#Field(() => ID)
#PrimaryGeneratedColumn()
id: number;
#Field({ nullable: true })
#Column({ nullable: true })
first_name?: string;
#Field({ nullable: true })
#Column({ nullable: true })
last_name?: string;
#Field()
#Column({ unique: true })
email: string;
#Column()
password: string;
#Field(() => [Book!])
#OneToMany(() => Book, book => book.created_by)
created_books: Book[];
}
For my resolver, it simply looks like this, which I am properly loading as the docs say.
#Resolver(Book)
export class OrthodontistResolver {
#Query(() => [Book])
books(): Promise<Book[]> {
return Book.find();
}
}
When I go into my GraphQL playground, and query something like this:
{
books {
name,
id,
}
}
It all works and returns the right data. However, when I try to use the created_by field like this:
{
orthodontists {
name,
id,
created_by {
id
}
}
}
It gives me the following error:
Cannot return null for non-nullable field Book.created_by.
I made sure the relation exists in the database, and set up correctly with it's FK's. Where does this come from though? How can I fix this? I did try using the #RelationId decorator, as seen in the first code example. It unfortunately didn't work.
EDIT:
There is only one book in the database, where the created_by field is not null.
Change your books resolver to return the relationship created_by when using find operation:
#Resolver(Book)
export class OrthodontistResolver {
#Query(() => [Book])
books(): Promise<Book[]> {
return Book.find({
relations: ["created_by"],
});
}
}

typeorm save in joined cloumn

in my project i have two entity, naturalist(user) and comment entity
i create relation between them, how to save reciever_id (joined cloumn) in comment entity
this is my comment entity
#Entity()
export class Comments extends BaseEntity {
#PrimaryGeneratedColumn()
id: string
#ManyToOne(
type => Naturalist,
naturalist => naturalist.comments
)
#JoinColumn({
name: 'naturalist_id',
})
naturalist: Naturalist
#Column({ nullable: true, default: '' })
text?: string
#Column({ nullable: true, default: '' })
sender_id?: string
}
naturalist(my user) entity
#Entity()
export class Naturalist extends BaseEntity {
#PrimaryGeneratedColumn()
id: number
#Column()
user_id: string
#OneToMany(
type => Comments,
comment => comment.naturalist
)
comments: Comments[]
}
DTO contains
{
receiver_id: '2cc2f359-821c-4940-99ae-7386576d861b',
text: 'string',
id: 'e0464049-d1d9-474a-af5b-815805aa1c4b'
}
i want to save receiver_id to my comment entity
if I understand correctly your needs,
I think you need an insert method to do this:
await entityManager.insert(Comments, { naturalist: { id: '2cc2f359-821c-4940-99ae-7386576d861b' } as Naturalist });

Resources