ufront / ufront-orm

The Object Relational Mapper, allows easy, type-safe access to your database. Builds on Haxe's SPOD but adds macro powered relationships, validation and client side usage
MIT License
13 stars 4 forks source link

Two ManyToMany with the same types? #22

Closed kevinresol closed 6 years ago

kevinresol commented 9 years ago

I am having a Event object which would like to have the following fields

public var participatingUsers:ManyToMany<Event, User>;
public var invitedUsers:ManyToMany<Event, User>;

But seems that the macro only generates one Join_Event_User table?

jasononeil commented 9 years ago

That's a limitation I've known about for some time but have avoided fixing.

I guess the time has come to fix it.

Would being able to specify a custom table name be an adequate solution?

@:table("event_participating_users") public var participatingUsers:ManyToMany<Event,User>
@:table("event_invited_users") public var invitedUsers:ManyToMany<Event,User>

Or do you have another suggestion? The extra metadata feels verbose so if there's a more elegant solution I'm open to it.

kevinresol commented 9 years ago

metadata is great for giving flexibility. If user does not specify table name, can we simply use the var name? Like _join_event_user_participatingUsers

kevinresol commented 9 years ago

or is it possible to use typedef?

typedef ParticipatingUser = User;
typedef InvitedUser = User;

// Event.hx
public var participatingUsers:ManyToMany<Event, ParticipatingUser>;
public var invitedUsers:ManyToMany<Event, InvitedUser>;

// User.hx
public var participatedEvents:ManyToMany<ParticipatingUser, Event>;
public var beingInvitedEvents:ManyToMany<InvitedUser, Event>;
jasononeil commented 9 years ago

Using the var name won't work because usually you'll use different var names on each of the models:

// Event.hx - would create tables "_join_event_user_participatingUsers" and "_join_event_user_invitedUsers"
public var participatingUsers:ManyToMany<User, ParticipatingUser>;
public var invitedUsers:ManyToMany<User, InvitedUser>;

// User.hx - would create tables "_join_event_user_participatedEvents" and "_join_event_user_beingInvitedEvents"
public var participatedEvents:ManyToMany<User, Event>;
public var beingInvitedEvents:ManyToMany<User, Event>;

The typedef idea is interesting. It could break unexpectedly if people are using typedef aliases, which I sometimes do when I rename a model:

// We renamed the model StaffMember, but I wanted to leave the typedef in place to prevent breaking code.
typedef Teacher = StaffMember;

I'm leaning towards the metadata idea unless you object... you are right that it gives you full flexibility, and I doubt it will be required often enough that you end up resenting writing the extra few characters.

And on the plus side it should be fairly easy to implement.

kevinresol commented 9 years ago

ok, agreed

kevinresol commented 6 years ago

Cleaning up my old issues and I think this is no longer needed.