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 composite primary keys #382

Closed
wants to merge 1 commit into from
Closed

Support composite primary keys #382

wants to merge 1 commit into from

Conversation

benjamincanac
Copy link
Contributor

Proposed changes

Support composite primary keys for updates and deletes.

Instead of updating how works static get primaryKey() to handle arrays and breaking everything, I added two new methods static get updatePrimaryKey() and deletePrimaryKey().

During update / delete the query is built accordingly to those two new methods. If it's an array, it allows you to save with composite primary keys.

This PR references #167.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have read the CONTRIBUTING doc
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate)

Further comments

Can you tell me what you think of this and if it's the way to go?

Also can you tell me where I can add tests for this feat?

@thetutlage
Copy link
Member

Hello @benjamincanac

Thanks for sharing the PR :)

I believe, we can use the primaryKey getter directly without introducing new properties. If the value of primaryKey is an array, we perform the multiple where logic.

Regarding breaking change, no existing project have been returning array from this getter, so it's safe to re-use the same getter.

Testing

The tests will go inside https://github.com/adonisjs/adonis-lucid/blob/develop/test/unit/lucid.spec.js and here's how you can construct them.

  1. Create a new table inside https://github.com/adonisjs/adonis-lucid/blob/develop/test/unit/helpers/index.js#L195 which relies on composite keys. Also make sure to drop the table after tests
  2. I believe a CRUD operation in 4-5 tests will cover the use cases for composite keys.

@benjamincanac
Copy link
Contributor Author

Hi @thetutlage

Thanks for your answer, I'm gonna close the PR and make another one with valid changes!

@RomainLanz
Copy link
Member

Hey @benjamincanac! 👋

Any news on this?

@benjamincanac
Copy link
Contributor Author

Hi @RomainLanz,

Actually I ended up by not doing it, the code was way too complex than with just an ID as a primary key.

@yariksav
Copy link

Need this enhancement

@scal-lider
Copy link

scal-lider commented May 25, 2021

Hello people,

Any new on this proposal?

List of related issues and discussion about this that I found:

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

Successfully merging this pull request may close these issues.

5 participants