Django db_index=True not creating index, but class Meta indexes yes












2















In my django model, when creating indexes via db_index=True in field definitions, the index is not created. Only if I created in the class Meta



class Agreement(UUIDPrimaryKey):
job = models.ForeignKey(
'posts.Job',
on_delete=models.CASCADE,
verbose_name=_("job"),
)
class Meta:
indexes = (
models.Index(fields=['job']),
)


And if I run makemigrations, the index is created.



Create index agreements__job_id_eb7df0_idx on field(s) job of model agreement


But If I change my model to:



class Agreement(UUIDPrimaryKey):
job = models.ForeignKey(
'posts.Job',
on_delete=models.CASCADE,
verbose_name=_("job"),
db_index=True,
)


And I run makemigrations, the index is deleted.



 Remove index agreements__job_id_eb7df0_idx from agreement


Should not be the same both definitions?



UPDATE



Documentation says they are the same. Yes both create index.. but if you create indexes in Meta, and do not specify db_index=False in the field definition, two indexes are created.



This is before setting db_index=False in the field



psql# select indexname from pg_indexes where tablename like 'agreemen%';

indexname
--------------------------------------------
agreements__job_id_eb7df0_idx
agreements_agreement_job_id_id_c26bd828
agreements_agreement_pkey


And after setting db_index=False and running migrations/migrate



           indexname           
-------------------------------
agreements__job_id_eb7df0_idx
agreements_agreement_pkey









share|improve this question





























    2















    In my django model, when creating indexes via db_index=True in field definitions, the index is not created. Only if I created in the class Meta



    class Agreement(UUIDPrimaryKey):
    job = models.ForeignKey(
    'posts.Job',
    on_delete=models.CASCADE,
    verbose_name=_("job"),
    )
    class Meta:
    indexes = (
    models.Index(fields=['job']),
    )


    And if I run makemigrations, the index is created.



    Create index agreements__job_id_eb7df0_idx on field(s) job of model agreement


    But If I change my model to:



    class Agreement(UUIDPrimaryKey):
    job = models.ForeignKey(
    'posts.Job',
    on_delete=models.CASCADE,
    verbose_name=_("job"),
    db_index=True,
    )


    And I run makemigrations, the index is deleted.



     Remove index agreements__job_id_eb7df0_idx from agreement


    Should not be the same both definitions?



    UPDATE



    Documentation says they are the same. Yes both create index.. but if you create indexes in Meta, and do not specify db_index=False in the field definition, two indexes are created.



    This is before setting db_index=False in the field



    psql# select indexname from pg_indexes where tablename like 'agreemen%';

    indexname
    --------------------------------------------
    agreements__job_id_eb7df0_idx
    agreements_agreement_job_id_id_c26bd828
    agreements_agreement_pkey


    And after setting db_index=False and running migrations/migrate



               indexname           
    -------------------------------
    agreements__job_id_eb7df0_idx
    agreements_agreement_pkey









    share|improve this question



























      2












      2








      2








      In my django model, when creating indexes via db_index=True in field definitions, the index is not created. Only if I created in the class Meta



      class Agreement(UUIDPrimaryKey):
      job = models.ForeignKey(
      'posts.Job',
      on_delete=models.CASCADE,
      verbose_name=_("job"),
      )
      class Meta:
      indexes = (
      models.Index(fields=['job']),
      )


      And if I run makemigrations, the index is created.



      Create index agreements__job_id_eb7df0_idx on field(s) job of model agreement


      But If I change my model to:



      class Agreement(UUIDPrimaryKey):
      job = models.ForeignKey(
      'posts.Job',
      on_delete=models.CASCADE,
      verbose_name=_("job"),
      db_index=True,
      )


      And I run makemigrations, the index is deleted.



       Remove index agreements__job_id_eb7df0_idx from agreement


      Should not be the same both definitions?



      UPDATE



      Documentation says they are the same. Yes both create index.. but if you create indexes in Meta, and do not specify db_index=False in the field definition, two indexes are created.



      This is before setting db_index=False in the field



      psql# select indexname from pg_indexes where tablename like 'agreemen%';

      indexname
      --------------------------------------------
      agreements__job_id_eb7df0_idx
      agreements_agreement_job_id_id_c26bd828
      agreements_agreement_pkey


      And after setting db_index=False and running migrations/migrate



                 indexname           
      -------------------------------
      agreements__job_id_eb7df0_idx
      agreements_agreement_pkey









      share|improve this question
















      In my django model, when creating indexes via db_index=True in field definitions, the index is not created. Only if I created in the class Meta



      class Agreement(UUIDPrimaryKey):
      job = models.ForeignKey(
      'posts.Job',
      on_delete=models.CASCADE,
      verbose_name=_("job"),
      )
      class Meta:
      indexes = (
      models.Index(fields=['job']),
      )


      And if I run makemigrations, the index is created.



      Create index agreements__job_id_eb7df0_idx on field(s) job of model agreement


      But If I change my model to:



      class Agreement(UUIDPrimaryKey):
      job = models.ForeignKey(
      'posts.Job',
      on_delete=models.CASCADE,
      verbose_name=_("job"),
      db_index=True,
      )


      And I run makemigrations, the index is deleted.



       Remove index agreements__job_id_eb7df0_idx from agreement


      Should not be the same both definitions?



      UPDATE



      Documentation says they are the same. Yes both create index.. but if you create indexes in Meta, and do not specify db_index=False in the field definition, two indexes are created.



      This is before setting db_index=False in the field



      psql# select indexname from pg_indexes where tablename like 'agreemen%';

      indexname
      --------------------------------------------
      agreements__job_id_eb7df0_idx
      agreements_agreement_job_id_id_c26bd828
      agreements_agreement_pkey


      And after setting db_index=False and running migrations/migrate



                 indexname           
      -------------------------------
      agreements__job_id_eb7df0_idx
      agreements_agreement_pkey






      django indexing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 30 '18 at 6:42







      Gonzalo

















      asked Dec 29 '18 at 2:40









      GonzaloGonzalo

      879




      879
























          1 Answer
          1






          active

          oldest

          votes


















          1














          A database index is automatically created on the ForeignKey. At least so says the documentation: https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey. So it is not necessary to set db_index=True in your ForeignKey field of your model. The option is still there incase you don't want an index (db_index=False).






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966256%2fdjango-db-index-true-not-creating-index-but-class-meta-indexes-yes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            A database index is automatically created on the ForeignKey. At least so says the documentation: https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey. So it is not necessary to set db_index=True in your ForeignKey field of your model. The option is still there incase you don't want an index (db_index=False).






            share|improve this answer






























              1














              A database index is automatically created on the ForeignKey. At least so says the documentation: https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey. So it is not necessary to set db_index=True in your ForeignKey field of your model. The option is still there incase you don't want an index (db_index=False).






              share|improve this answer




























                1












                1








                1







                A database index is automatically created on the ForeignKey. At least so says the documentation: https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey. So it is not necessary to set db_index=True in your ForeignKey field of your model. The option is still there incase you don't want an index (db_index=False).






                share|improve this answer















                A database index is automatically created on the ForeignKey. At least so says the documentation: https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey. So it is not necessary to set db_index=True in your ForeignKey field of your model. The option is still there incase you don't want an index (db_index=False).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 29 '18 at 3:24

























                answered Dec 29 '18 at 3:10









                Red CricketRed Cricket

                4,36993384




                4,36993384






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966256%2fdjango-db-index-true-not-creating-index-but-class-meta-indexes-yes%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas