Skip to content

Support for composite keys #8

@trurl-master

Description

@trurl-master

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions