Django doesn't recognize the sequences/triggers in existing Oracle DB

309 views
Skip to first unread message

Wanpeng

unread,
Jul 3, 2009, 8:13:50 AM7/3/09
to django...@googlegroups.com
Hello,
I am writing my first app in Django. I already have existing tables/sequences/triggers in Oracle. so I used "inspectdb" to generate the models for django. Here's one model for example:

class Instructors(models.Model):
    instructorid = models.IntegerField(db_column='INSTRUCTORID', primary_key=True) # Field name made lowercase.
    instructor_name = models.CharField(max_length=50, db_column='INSTRUCTOR_NAME', blank=True) # Field name made lowercase.
    instructor_email = models.CharField(max_length=255, db_column='INSTRUCTOR_EMAIL', blank=True) # Field name made lowercase.
    comments = models.CharField(max_length=255, db_column='COMMENTS', blank=True) # Field name made lowercase.
    fingerprint = models.CharField(max_length=8, db_column='FINGERPRINT', blank=True) # Field name made lowercase.
    timestamp = models.DateField(null=True, db_column='TIMESTAMP', blank=True) # Field name made lowercase.
   
    def __unicode__(self):
        return self.instructor_name
    class Meta:
        db_table = u'INSTRUCTORS'

After "syncdb", here's what I do in the shell:

 >>>from mysite.assignments.models import Instructors
 >>>p = Instructors(instructor_
name="John",instructor_email = "Jo...@email.com")
 >>>p.save()

I already have a trigger in oracle to insert the instructorid, the fingerprint and timestamp. However, when I type:
>>>p.instructorid

It returns null while it should return a new instructorid. If I change the model type of instructorid to AutoField, I get the 'Cannot find sequence' error when using p.save() , but I do have the sequence and trigger.

I tried to create a new model in django and let django create the table for me in oracle. I don't have the same problem as with the existing tables.

So how can I let django know 'I already have sequences and triggers, use them!' ?

Thank you.

Ian Kelly

unread,
Jul 3, 2009, 5:33:21 PM7/3/09
to Django users
> Jo...@email.com <wanpeng.y...@gmail.com>")
>  >>>p.save()
>
> I already have a trigger in oracle to insert the instructorid, the
> fingerprint and timestamp. However, when I type:
>
> >>>p.instructorid
>
> It returns null while it should return a new instructorid. If I change the
> model type of instructorid to AutoField, I get the 'Cannot find sequence'
> error when using p.save() , but I do have the sequence and trigger.
>
> I tried to create a new model in django and let django create the table for
> me in oracle. I don't have the same problem as with the existing tables.
>
> So how can I let django know 'I already have sequences and triggers, use
> them!' ?
>
> Thank you.

Hi Wanpeng,

Unfortunately, the Oracle backend doesn't currently offer a way to
specify an alternative name for the sequence. However, in Django 1.1
we have changed the way AutoField values are retrieved to use a
"RETURNING" clause, which in addition to being more efficient does not
require knowledge of the sequence name. I would suggest upgrading to
the latest trunk version if possible.

If upgrading is not an option, then I suggest creating a synonym for
the sequence with the name Django expects for the sequence. This is
normally the name of the table plus "_SQ", unless that would be longer
than the allowed 30 characters.

Hope this helps,
Ian

Wanpeng

unread,
Jul 3, 2009, 7:26:02 PM7/3/09
to django...@googlegroups.com
Thanks Ian!

I'll try the new version first.

Wanpeng
Reply all
Reply to author
Forward
0 new messages