Skip to content
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

Support for composite keys #8

Open
trurl-master opened this issue Mar 1, 2018 · 0 comments
Open

Support for composite keys #8

trurl-master opened this issue Mar 1, 2018 · 0 comments

Comments

@trurl-master
Copy link
Contributor

Currently, the only way to identify a resource is by ID, I tried to imagine how it may work for composite keys (and non-ID keys in general) and made some preliminary work on the subject in this commit but it raises some questions regarding how it should be used.

Let's consider this db:

<database name="bookstore" defaultIdMethod="native">
  <table name="book">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
  </table>
  <table name="book_i18n">
    <column name="id" type="integer" required="true" primaryKey="true" />
    <column name="locale" type="varchar" length="5" required="true" primaryKey="true" />
    <column name="title" type="varchar" length="512" required="true" primaryString="true" />

    <column name="id" type="integer" />
    <foreign-key foreignTable="book" onDelete="CASCADE">
        <reference local="id" foreign="id"/>
    </foreign-key>
  </table>
</database>

That's what Propel's i18n behavior generates.

So the simplest way I see is this:

db.book_i18ns(1, 'en_US').setTitle('Cold Winds').save()

Which generates this resource URL: /book_i18ns/1/en_US/
This is the way how it's done in the mentioned commit and it works quite nicely.

Or we can try and play with foreign keys:

db.books(1).book_i18ns('en_US').setTitle('Cold Winds').save()

It can generate something like: /books/1/book_i18ns/en_US/
Maybe it's more correct, i don't know. What do you think?

In both cases, there's an issue (#7) with creating the record in the known location.

And all that raises one more issue with getting the resourceName, at present, you can easily figure it out because it's either at the end (if it's not numeric) or before the id. If the number of primary keys is unknown then you either have to send that number alongside the request or use another method of figuring out the resource name, for the moment I chose the latter and count from the beginning of the URL. That's the only breaking change, you have to specify the root index, eg:

API::handle(1);

for URLs like /api/{:resourceName}/...

I would very much like your input on all that, maybe I'm completely off and you already have something better in mind. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant