question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add index to RelationOptions

See original GitHub issue

Issue type:

[ ] question [ ] bug report [x] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

It would be nice to be able to define in a relation to automatically create an index for it. I think that the best place to put it would in the RelationOptions.

Today it is possible to get the desired result doing the following:

import {Entity, PrimaryGeneratedColumn, Column, ManyToOne, Index} from "typeorm";
import {User} from "./User";

@Entity()
export class Photo {
    
    @PrimaryGeneratedColumn()
    id: number;
    
    @ManyToOne(type => User, user => user.photos)
    user: User;

    @Index
    @RelationId((user: User) => user.photos)
    userId: number;
}

The idea was to be able to do the same with the following code:

import {Entity, PrimaryGeneratedColumn, Column, ManyToOne, Index} from "typeorm";
import {User} from "./User";

@Entity()
export class Photo {
    
    @PrimaryGeneratedColumn()
    id: number;
    
    @ManyToOne(type => User, user => user.photos, { index: true })
    user: User;
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
pleerockcommented, Jul 28, 2018

indices have high cost, and should be used carefully, we can’t decide for users if they should pay for it on each relation or not. They should decide it on their own.

3reactions
leonidasvcommented, Jan 21, 2021

For those who may be struggling with the same issue, I’ve managed to make it work by adding the @Index() decorator right to the relation and removing it from the RelationId:

   @Index()
   @ManyToOne(type => User, user => user.photos)
   user: User;

   @RelationId((photo: Photo) => photo.user)
   userId: number;

It generated the migration:

queryRunner.query(`CREATE INDEX "IDX_dddc92fd77a1b1682a8d6fb07d" ON "photo" ("userId")`)

(the hash after IDX_ is illustrative)

Read more comments on GitHub >

github_iconTop Results From Across the Web

RelationOptions | typeorm
Describes all relation's options. Hierarchy. RelationOptions. Index. Properties. cascade · eager ...
Read more >
Relations - typeorm - GitBook
Relation options. There are several options you can specify for relations: eager: boolean - If set to true, the relation will always be...
Read more >
typeorm - create a primary key for a one-to-one relationship
I thought I had found the solution, but this syntax seems outdated; the RelationOptions does not contain the primary property:
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
More information on supported column types can be found here. # Creating a new DataSource. Now, when our entity is created, let's create...
Read more >
Indexes - EF Core | Microsoft Learn
Configuring indexes in an Entity Framework Core model. ... Attempting to insert more than one entity with the same values for the index's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found