Add index to RelationOptions
See original GitHub issueIssue 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:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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 theRelationId
:It generated the migration:
(the hash after
IDX_
is illustrative)