Note that there are some explanatory texts on larger screens.

plurals
  1. POComposite OneToOneFields in Django
    text
    copied!<p>I am fairly new to Django and I think I pretty much get the basic idea of ORM. However, there is a peculiar situation to which I do not see a plausible solution. I have a legacy database for which I am trying to write a Django app. The sql structure of both the tables is:</p> <pre><code>mysql&gt; describe event; +-----------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+------------------+------+-----+---------+-------+ | sid | int(10) unsigned | NO | PRI | NULL | | | cid | int(10) unsigned | NO | PRI | NULL | | | signature | int(10) unsigned | NO | MUL | NULL | | | timestamp | datetime | NO | MUL | NULL | | +-----------+------------------+------+-----+---------+-------+ mysql&gt; describe alerts; +----------------+-----------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+-----------+------+-----+-------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | sid | int(11) | YES | MUL | NULL | | | cid | int(11) | YES | | NULL | | | confidence | int(11) | YES | | NULL | | | cvss_base | float | YES | | NULL | | | composite_conf | float | YES | | NULL | | +----------------+-----------+------+-----+-------------------+----------------+ </code></pre> <p>The first table cannot be altered because it would break a lot of code (which I have not written). The second table was written by me (hence the surrogate key).In the second ('alerts') table, (sid,cid) is unique.</p> <p>The problem is that (sid,cid) is the key on which tables can be effectively joined. How should the models be re-written so that Django can accurately capture the relation between the two tables? I tried OnetoOne for both sid and cid separately but that is clearly not useful since OnetoOne should apply on (sid,cid) simultaneously. OnetoMany is also of no use and neither is ForeignKey since I need (sid,cid) to be the foreign key. It appears that (sid,cid) composite field should be OnetoOne but I don't know how to achieve that.</p> <p>Note - (sid,cid) value will be of the type 1-55,2-55,3-55,1-56,2-57,3-60 etc. all of which are unique and will have only one entry in both the tables.</p> <p>Late EDIT: I guess Django does not support Composite onetoone keys (atleast that's what one of the guys on google-group said). I migrated to SQLalchemy and it represents the model exactly as I want it (Composite keys etc)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload