#12421: Foreign Key on Non-Primary Field fails due to lack of Index on Related
Field w/ MySQL
---------------------------------------------------+------------------------
          Reporter:  anonymous                     |         Owner:  nobody
            Status:  closed                        |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN   
        Resolution:  worksforme                    |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  0     
        Needs_docs:  0                             |   Needs_tests:  0     
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by erikr):

  * status:  reopened => closed
  * resolution:  => worksforme

Comment:

 I can not reproduce this with your model with Django trunk, because
 syncdb/sqlall refuse db_index=True unless I also set unique=True on
 License.name.


 {{{
 Error: One or more models did not validate:
 foo.item: Field 'name' under model 'License' must have a unique=True
 constraint.
 }}}


 Having written the model like this:
 {{{
 from django.db import models

 class Item(models.Model):
     license = models.ForeignKey('License', to_field="name", null=True)

     class Meta:
         db_table = 'testo_item'

 class License(models.Model):
     name = models.PositiveIntegerField(db_index=True)

     class Meta:
         db_table = 'testo_license'
 }}}

 The SQL becomes this, which is valid for InnoDB:
 {{{
 BEGIN;
 CREATE TABLE `testo_item` (
     `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
     `license_id` integer UNSIGNED
 )
 ;
 CREATE TABLE `testo_license` (
     `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
     `name` integer UNSIGNED NOT NULL UNIQUE
 )
 ;
 ALTER TABLE `testo_item` ADD CONSTRAINT `license_id_refs_name_a4fa988`
 FOREIGN KEY (`license_id`) REFERENCES `testo_license` (`name`);
 CREATE INDEX `testo_item_139668fe` ON `testo_item` (`license_id`);
 COMMIT;
 }}}

 The code will simply not allow me to set db_index unless I set unique.

 Closing this again as worksforme.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/12421#comment:4>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to