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

Named relationships updated #182

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
6 participants
@sebastiansen

sebastiansen commented Oct 13, 2013

Support for named relationships, passing a vector optionally with name and entity instead of only the entity. This will also allow more than one relationship between two entities. e.g.

(defentity users
(belongs-to [:book books]))

(select users
(with :book))

(select users
(join :book))

@sebastiansen sebastiansen referenced this pull request Oct 13, 2013

Closed

Named relationships #125

@AlexJuarez

This comment has been minimized.

Show comment
Hide comment
@AlexJuarez

AlexJuarez commented Oct 13, 2013

Nice!

@bitemyapp

This comment has been minimized.

Show comment
Hide comment
@bitemyapp

bitemyapp Oct 17, 2013

Member

Could you explain or demonstrate a use-case for this where it's needed?

Member

bitemyapp commented Oct 17, 2013

Could you explain or demonstrate a use-case for this where it's needed?

@AlexJuarez

This comment has been minimized.

Show comment
Hide comment
@AlexJuarez

AlexJuarez Oct 17, 2013

The simple use case is pluralization, for example in rails, you can say has-one book or has-many books. And both would be correct, this would allow for the same kind of thought, Additionally, checkout the lobos, https://github.com/budu/lobos (migration) example it is done with pluralization a convention which is currently difficult to do with korma.

;; This code here on the lobos md
(defn refer-to [table ptable]
  (let [cname (-> (->> ptable name butlast (apply str))
                  (str "_id")
                  keyword)]
    (integer table cname [:refer ptable :id :on-delete :set-null])))

AlexJuarez commented Oct 17, 2013

The simple use case is pluralization, for example in rails, you can say has-one book or has-many books. And both would be correct, this would allow for the same kind of thought, Additionally, checkout the lobos, https://github.com/budu/lobos (migration) example it is done with pluralization a convention which is currently difficult to do with korma.

;; This code here on the lobos md
(defn refer-to [table ptable]
  (let [cname (-> (->> ptable name butlast (apply str))
                  (str "_id")
                  keyword)]
    (integer table cname [:refer ptable :id :on-delete :set-null])))
@bitemyapp

This comment has been minimized.

Show comment
Hide comment
@bitemyapp

bitemyapp Oct 17, 2013

Member

@AlexJuarez the code bloat has to be justified on the basis of something more functional and useful than an alias to an existing relationship so that your has-one/has-many reads a little nicer.

Member

bitemyapp commented Oct 17, 2013

@AlexJuarez the code bloat has to be justified on the basis of something more functional and useful than an alias to an existing relationship so that your has-one/has-many reads a little nicer.

@sebastiansen

This comment has been minimized.

Show comment
Hide comment
@sebastiansen

sebastiansen Oct 17, 2013

A not very good solution for this was in #67, but i consider a problem if you have to create an entity to represent a relationship, and maintain the same attributes for each one.

sebastiansen commented Oct 17, 2013

A not very good solution for this was in #67, but i consider a problem if you have to create an entity to represent a relationship, and maintain the same attributes for each one.

@AlexJuarez

This comment has been minimized.

Show comment
Hide comment
@AlexJuarez

AlexJuarez Oct 17, 2013

@sebastiansen #67 well that answers all my questions.

AlexJuarez commented Oct 17, 2013

@sebastiansen #67 well that answers all my questions.

@bitemyapp

This comment has been minimized.

Show comment
Hide comment
@bitemyapp

bitemyapp Oct 17, 2013

Member

What about this #67 (comment) doesn't work?

Member

bitemyapp commented Oct 17, 2013

What about this #67 (comment) doesn't work?

@lokori

This comment has been minimized.

Show comment
Hide comment
@lokori

lokori Nov 27, 2013

Contributor

The point is that it is a common case to have two relationships between entities which have different logical semantics. As a real word example here is our current case right now:

Contract - Official

There are two semantically different relationships:

  1. Contract is handled (governed) by one Official
  2. Contract has been originally signed by one Official.

The obvious way to model this in the relational database is to make two foreign key references which are named in a manner which documents what they actually mean:

create table contract
  ..
  governor references Official
  signee references Official

As I said, this is a common case and Korma should support it without resorting to awkward workarounds like #67 . If the multiple comments here are not enough justification for that argument then what is? I can't prove it to you based on some objective criteria.

Contributor

lokori commented Nov 27, 2013

The point is that it is a common case to have two relationships between entities which have different logical semantics. As a real word example here is our current case right now:

Contract - Official

There are two semantically different relationships:

  1. Contract is handled (governed) by one Official
  2. Contract has been originally signed by one Official.

The obvious way to model this in the relational database is to make two foreign key references which are named in a manner which documents what they actually mean:

create table contract
  ..
  governor references Official
  signee references Official

As I said, this is a common case and Korma should support it without resorting to awkward workarounds like #67 . If the multiple comments here are not enough justification for that argument then what is? I can't prove it to you based on some objective criteria.

@hxzon

This comment has been minimized.

Show comment
Hide comment
@hxzon

hxzon Jan 5, 2015

给关联实体添加别名

hxzon commented Jan 5, 2015

给关联实体添加别名

@venantius

This comment has been minimized.

Show comment
Hide comment
@venantius

venantius Feb 27, 2018

Contributor

It has been over 3 years since the last comment on this issue. I would argue that cases like this in which you have multiple semantic relationships to likely be better addressed by using explicit joins rather than relying on the entity definition. Alternatively, in @lokori's example, you could just define two separate entities and connect them to the same table but with different relationship semantics. As there are at least two reasonable ways of addressing this, I'm going to close this PR for now.

Contributor

venantius commented Feb 27, 2018

It has been over 3 years since the last comment on this issue. I would argue that cases like this in which you have multiple semantic relationships to likely be better addressed by using explicit joins rather than relying on the entity definition. Alternatively, in @lokori's example, you could just define two separate entities and connect them to the same table but with different relationship semantics. As there are at least two reasonable ways of addressing this, I'm going to close this PR for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment