-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can't set ManyToOne columns as primary 0.3.0 #3238
Comments
just define a primary column you need, for example: @PrimaryColumn()
typeId: number
@ManyToOne(() => Type) // you can also specify a primary column name, e.g. @JoinColumn({ name: "typeId" })
type: Type This change was made to simplify lot of things and fix some serious bugs. |
@pleerock following your example: @Entity()
export class Order extends AutoMeta {
@PrimaryColumn()
id: string;
// ...
@OneToMany(() => Item, item => item.order, { cascade: true, eager: true })
items: Item[]
}
@Entity()
export class Item {
@ManyToOne(() => Order, order => order.items)
order: Order;
// Use orderId+productId as a composite primary key. PrimaryColumn decoration on ManyToOne directly does not work; need to list the resulting orderId explicitly.
@PrimaryColumn()
orderId: string;
@PrimaryColumn()
productId: string;
// ...
}
// somewhere else
await dbm.save(Order, orders); This works once on an empty database, but then fails with |
Changing it to this: @Entity()
export class Item {
@ManyToOne(() => Order, order => order.items, { primary: true })
order: Order;
@PrimaryColumn()
productId: string;
// ...
} made it work:
until I deleted the database - then this approach fails with |
Using the first approach, setting orderId explicitly, it fails with |
I believe he wanted the |
I'm also interested on it |
+1 |
1 similar comment
+1 |
I experienced the same issue in the following scenario: when a table has composite primary keys you have to specify both in the @joincolumn.
The issue is that Phase entity had composite primary key, thus I needed to change the second (dependency relation to this):
|
I'm in the same boat, it should be possible to do FK and PK on the same column, did you guys figure out in the end of the day on how to do that correctly in typeorm? the accepted solution seem to be reporting issues, was it fixed? |
I don't understand : why is this issue closed when there is no solution to this ? @pleerock |
issue too |
You can add the @ManyToOne(() => EventLeaderboard, {
cascade: false,
eager: false,
nullable: false,
onDelete: 'CASCADE',
})
@JoinColumn({
name: 'event_window_id',
referencedColumnName: 'eventWindow',
foreignKeyConstraintName: 'FK_event_leaderboard_entry_event_window_id',
})
@PrimaryColumn({
name: 'event_window_id',
type: 'varchar',
length: 255,
primaryKeyConstraintName: 'PK_event_leaderboard_entry_id',
})
leaderboard: EventLeaderboard;
@ManyToOne(() => Team, (team) => team.eventLeaderboardEntry, {
cascade: false,
eager: false,
nullable: false,
onDelete: 'RESTRICT',
})
@JoinColumn({
name: 'team_id',
referencedColumnName: 'teamId',
foreignKeyConstraintName: 'FK_event_leaderboard_entry_team_id',
})
@PrimaryColumn({
name: 'team_id',
type: 'text',
primaryKeyConstraintName: 'PK_event_leaderboard_entry_id',
})
team: Team; This is an example of composite primary keys, both Now, as for @griebdaniel, when referencing multiple columns as an array in the @PrimaryColumn({
name: 'event_window_id',
type: 'varchar',
length: 255,
primaryKeyConstraintName: 'PK_event_session_id',
})
eventWindowId: string;
@PrimaryColumn({
name: 'team_id',
type: 'text',
primaryKeyConstraintName: 'PK_event_session_id',
})
teamId: string;
@ManyToOne(() => EventLeaderboardEntry, {
cascade: false,
eager: false,
nullable: false,
onDelete: 'CASCADE',
})
@JoinColumn([
{
name: 'event_window_id',
referencedColumnName: 'leaderboard',
foreignKeyConstraintName: 'FK_event_session_event_window_id',
},
{
name: 'team_id',
referencedColumnName: 'team',
foreignKeyConstraintName: 'FK_event_session_team_id',
},
])
eventLeaderboardEntry: EventLeaderboardEntry; Although unnecessary in the business logic side, they're mainly used for TypeORM to create the correct syntax when generating SQL migrations. |
Issue type:
[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[x]
postgres
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
[ ]
expo
TypeORM version:
[ ]
latest
[x]
@next
[ ]
0.x.x
(or put your version here)Steps to reproduce or a small repository showing the problem:
@ManyToOne(type => Type, { primary: true })
error: primary is not an available option anymore.
I cannot set a column primary for @manytoone columns. It worked for 0.2.9. Why this was removed?
The text was updated successfully, but these errors were encountered: