简体   繁体   中英

Possible to DELETE or UPDATE on junction table w/ no primary key

I have a junction table called PlanConflicts , which allows for a many-to-many relationship between plans and conflicts.

Here is my model definition:

import { AllowNull, Column, DataType, ForeignKey, Model, Table } from "sequelize-typescript";
import Conflict from "./Conflict";
import Plan from "./Plan";

@Table({ tableName: "PlanConflicts" })
export default class PlanConflict extends Model<PlanConflict> {
    @AllowNull(false)
    @ForeignKey(() => Plan)
    @Column(DataType.INTEGER)
    public planId: number;

    @AllowNull(false)
    @ForeignKey(() => Conflict)
    @Column(DataType.INTEGER)
    public conflictId: number;
}

Here is my attempt to generate endpoints for this table using epilogue:

epilogue.resource({
    model: PlanConflict,
    endpoints: ["/plan-conflicts", "/plan-conflicts/:id"],
    pagination: false,
    search: [
        {
            operator: "$eq",
            param: "plan-id",
            attributes: [ "planId" ]
        },
        {
            operator: "$eq",
            param: "conflict-id",
            attributes: [ "conflictId" ]
        }
    ]
});

I'm able to create a new conflict, and I can get a listing using a URL of the form http://localhost:3000/plan-conflicts?plan-id=9&conflict-id=1 , but I can't figure out how I would perform an update or delete. The search feature of epilogue is only available for listings.

So, I'd like to know if it's possible to update or delete an item for a junction table that has no primary key, and if so, how.

After a couple hours of rest, it dawned on me that I might just be able to just customize the endpoint from .../:id to .../:planId/:conflictId and sure enough, that did the trick.

My resource call is now simplified to:

epilogue.resource({
    model: PlanConflict,
    endpoints: ["/plan-conflicts", "/plan-conflicts/:planId/:conflictId"],
    pagination: false,
});

And here is an example URL: http://localhost:3000/plan-conflicts/9/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM