Code

Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#9525 closed (wontfix)

It should be possible to disable the creation of database key constraints in ForeignKey Field

Reported by: mohlendo Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I have the following model:

class A(models.Model):
  """
  A alias_name of 'A' can have various name
  """
  name = models.CharField(max_length=50)
  alias_name = models.CharField(max_length=50)

class B(models.Model):
   a = models.ForeignKey(A, related_name='bs', to_field='alias_name', db_column='a_name', create_constraint=False)
   user = models.ForeignKey(User, related_name='bs')

During syncdb the ForeignKey from A to B should not be created as a key constraint. Since B has a foreign key to the alias_name of A and one alias_name is mappend to various names, B gets all As with a specific alias_name. This is more or less a n-m relation without the extra table.

See attached patch...

Attachments (1)

django_foreign_key.patch (1.9 KB) - added by mohlendo 6 years ago.
simple patch for this ticket

Download all attachments as: .zip

Change History (3)

Changed 6 years ago by mohlendo

simple patch for this ticket

comment:1 Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

I think that if you want to do this, you should create a special field to do so. A ForeignKey is traditionally understood as modelling a many-to-one relation, not many-to-many, and that's certainly the case in Django. Extending the meaning beyond that would confuse things a bit.

It's an interesting idea, but not really how a database models things, so you will need to do some manual validation in your custom field to ensure data integrity.

comment:2 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.