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
Fixed #26709 -- Add class based indexes and CreateIndex operation #6726
Conversation
tablespace_sql = " " + tablespace_sql | ||
|
||
if not hasattr(self, 'name'): | ||
self.name = schema._create_index_name(self.model, columns, suffix=suffix) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this is the right place to do this. Please have a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think this would be an obvious extension point for a subclass. Perhaps get_name()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I write a new method get_name
for Index
class, which would create a name for the index independent of the schema. That way we will also have a uniform naming standard for the indexes, no matter which database the user uses.
But we might have to think about backward compatibility for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name should be generated as part of the index class and should be database backend independent, otherwise your migrations would yield different names depending on the database used to generate the migrations.
I think this is broadly heading in the right direction. I'd like to see some example migration file code. Perhaps it's worth you setting up a simple project that we can build up the functionality but makes it easy to show that it works. I think |
About naming, we have: It would be nice if the new operations were consistent with these names. I guess I'd go with AddIndex/RemoveIndex since, like fields, these are ancillary things to models -- other opinions? |
|
||
.. versionadded:: 1.11 | ||
|
||
Creates a new index in the database table for the given model. ``model_name`` is the model's name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please wrap documentation lines at 79 characters
Agreed. |
The names are derived from the SQL syntax which is |
model_state = state.models[app_label, self.model_name.lower()] | ||
if not hasattr(self.index, 'model'): | ||
self.index.model = state.apps.get_model(app_label, self.model_name) | ||
if 'indexes' not in model_state.options: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should never be the case. ModelState
should have a default indexes = []
Well there we go, I don't even recall the names of the operations: it's |
As indexes are related to a table/model I find Is there a reason not to use a single |
@@ -17,4 +17,5 @@ Model API reference. For introductory material, see :doc:`/topics/db/models`. | |||
lookups | |||
expressions | |||
conditional-expressions | |||
indexes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably add this right after fields
I have updated the algorithm. |
if index_name[0] == "_": | ||
index_name = index_name[1:] | ||
# It can't start with a number on Oracle, so prepend D if we need to | ||
if index_name[0].isdigit(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add validation that user-provided names don't start with a digit?
Some cosmetic edits at akki#2. |
@timgraham Added tests for index names starting with numbers and underscores (regarding |
|
||
def state_forwards(self, app_label, state): | ||
model_state = state.models[app_label, self.model_name.lower()] | ||
if not hasattr(self.index, 'model'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this if statement can be removed as it doesn't seem to ever evaluate to False when running the test suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does for new Index
instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I forgot the not
. Done.
Added corresponding migration operations - AddIndex, RemoveIndex to use them Thanks to markush, mjtamlyn, timgraham and charettes for reviewing and all the discussions.
Updated PR addressing all comments. Rebased against master and squashed all commits. |
merged in 156e2d5, thanks! |
Ticket #26709.