-
Hi! (Note sure it this has been brought up before) The current docs states that Composite Key support works for hasOne, belongsTo & hasMany relations. I'm wondering if belongsToMany also supports this (if so, is something wrong with my code?). If not, is there a way to handle many to many relations with a composite key?. My models look like these: class Child extends Model {
static entity = "children";
static primaryKey = ["field1", "field2"];
@Str() declare field1: string;
@Str() declare field2: string;
@Str() declare foo: string;
};
class Parent extends Model {
static entity = "parents";
static primaryKey = "username";
@Str() declare username: string;
@BelongsToMany(
() => Child,
() => Relation,
"parentId",
["childKey1", "childKey2"],
"username",
["field1", "field2"]
) declare related: Child[];
};
class Relation extends Model {
static entity = "relations";
static primaryKey = ["parentId", "childKey1", "childKey2"];
@Str() declare parentId: string;
@Str() declare childKey1: string;
@Str() declare childKey2: string;
}; So far, after saving a Parent with related (via
And the pivot object is automatically generated as: Object {
"childKey1,childKey2": undefined,
parentId: "someRandomIdProvidedByTheUser",
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @KYSXD ,
That is the state. A cup of ☕ helps me a lot to be motivated implementing it 😉 if you need it. |
Beta Was this translation helpful? Give feedback.
Hey @KYSXD ,
That is the state.
BelongsToMany
doesn't support composite key. I thought it wouldn't be used for that and it's compicated to implement so that's why it isn't implemented forbelongToMany
yet.A cup of ☕ helps me a lot to be motivated implementing it 😉 if you need it.