Opened 15 years ago

Closed 15 years ago

#11738 closed (invalid)

many2many recursive relationship do not work in DJ 1.1 - second problem

Reported by: simon_gray99 Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords: many2many recursive missing tables
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alex Gaynor)

According to the page at:
http://www.djangoproject.com/documentation/models/m2m_recursive/

All that is required to produce a recursive many2many is the following code:

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=20)
    friends = models.ManyToManyField('self')
    idols = models.ManyToManyField('self', symmetrical=False, related_name='stalkers')

    def __unicode__(self):
        return self.name

When I try this exact same code in my application,

I get an immediate complaint that the table 'persons_idols' does not exist.

If I CREATE this table manually then things are OK but the documentation state that DJ is supposed to handle creating this
table when the 'through' flag is NOT used... it doesn't!

Please advise

Change History (7)

comment:1 by simon_gray99, 15 years ago

In addition, if I CREATE the table needed in my model (in this case 'persons_idols')
then I am unable to create a test database with the following error:

Error: Error: idj couldn't be reset. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlreset idj'. That's the SQL this command wasn't able to run.
The full error: relation "persons_idols" does not exist

even although I had created the table. Examining the SQL in more detail, I find that it is trying to DROP the table before it has created it... and therefore failing, of course. WTF?

So it seems like either I have to create the tables by hand in order to get the model to work, and give up automated testing,
or I have to skip recursive many2many links completely and somehow find another way through... :(

Please can someone point me in an effective direction for a solution?

comment:2 by Alex Gaynor, 15 years ago

Description: modified (diff)

Please use the preview button.

comment:3 by simon_gray99, 15 years ago

Component: UncategorizedDatabase layer (models, ORM)

here is the full traceback from my machine at home:

In [1]: from isuite.models import Person

In [2]: a = Person(name='Anne')

In [3]: a.save()
---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)

/Volumes/home/Users/simon/isuite/<ipython console> in <module>()

/Library/Python/2.5/site-packages/django/db/models/base.pyc in save(self, force_insert, force_update)
    408             raise ValueError("Cannot force both insert and updating in "
    409                     "model saving.")
--> 410         self.save_base(force_insert=force_insert, force_update=force_update)
    411 
    412     save.alters_data = True

/Library/Python/2.5/site-packages/django/db/models/base.pyc in save_base(self, raw, cls, origin, force_insert, force_update)
    493                 if values:
    494                     # Create a new record.
--> 495                     result = manager._insert(values, return_id=update_pk)
    496                 else:
    497                     # Create a new record with defaults for everything.

/Library/Python/2.5/site-packages/django/db/models/manager.pyc in _insert(self, values, **kwargs)
    175 
    176     def _insert(self, values, **kwargs):
--> 177         return insert_query(self.model, values, **kwargs)
    178 
    179     def _update(self, values, **kwargs):

/Library/Python/2.5/site-packages/django/db/models/query.pyc in insert_query(model, values, return_id, raw_values)
   1083     part of the public API.
   1084     """
   1085     query = sql.InsertQuery(model, connection)
   1086     query.insert_values(values, raw_values)
-> 1087     return query.execute_sql(return_id)

/Library/Python/2.5/site-packages/django/db/models/sql/subqueries.pyc in execute_sql(self, return_id)
    318     def execute_sql(self, return_id=False):
    319         self.return_id = return_id
--> 320         cursor = super(InsertQuery, self).execute_sql(None)
    321         if not (return_id and cursor):
    322             return

/Library/Python/2.5/site-packages/django/db/models/sql/query.pyc in execute_sql(self, result_type)
   2367                 return
   2368         cursor = self.connection.cursor()
-> 2369         cursor.execute(sql, params)
   2370 
   2371         if not result_type:

/Library/Python/2.5/site-packages/django/db/backends/util.pyc in execute(self, sql, params)
     17         start = time()
     18         try:
---> 19             return self.cursor.execute(sql, params)
     20         finally:
     21             stop = time()

ProgrammingError: relation "isuite_person" does not exist

comment:4 by simon_gray99, 15 years ago

I think I may have found the problem... despite me changing my model, running 'manage.py syncdb' is refusing to notice the model has changed
and is therefore not creating the tables.

Currently my application is laid out as follows
<base>/idj/models.py
<base>/djsite/...

It appears to be missing the 'models.p'. file completely and failing silently
despite the fact that 'manage.py runserver' works perfectly....

There is some inconsistency here... perhaps someone can explain?

comment:5 by simon_gray99, 15 years ago

I think I may have found the problem... despite me changing my model, running 'manage.py syncdb' is refusing to notice the model has changed and is therefore not creating the tables.

Currently my application is laid out as follows:

<base>/idj/models.py
<base>/djsite/...

It appears to be missing the 'models.py'. file completely and failing silently despite the fact that 'manage.py runserver' works perfectly....

There is some inconsistency here... perhaps someone can explain?

comment:6 by James Bennett, 15 years ago

syncdb, as documented, only does the initial table creation at the time you first define the model class. It does not make changes to your database later based on changes to models. Please consult the Django documentation for further details.

comment:7 by James Bennett, 15 years ago

Resolution: invalid
Status: newclosed

(closing as invalid now that it's clear this isn't a Django bug)

Note: See TracTickets for help on using tickets.
Back to Top