Hi Ian

Sorry I wasn't really clear in what I want to achieve. I really just
want to call my primary key, in this case, employee_id and have it
incremented automatically.

I tried the following:

from django.db import models

class Employee(models.Model):
    employee_id = models.AutoField(max_length=10, primary_key=True)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)

and got this output - the table and sequence were created but not the
trigger.


C:\DjangoTraining\PK>python manage.py validate
0 errors found.

C:\DjangoTraining\PK>python manage.py sql test
CREATE TABLE "TEST_EMPLOYEE" (
    "EMPLOYEE_ID" NUMBER(11) NOT NULL PRIMARY KEY,
    "FIRST_NAME" NVARCHAR2(20) NULL,
    "LAST_NAME" NVARCHAR2(20) NULL
)
;
CREATE SEQUENCE TEST_EMPLOYEE_SQ;
CREATE OR REPLACE TRIGGER TEST_EMPLOYEE_TR
  BEFORE INSERT ON "TEST_EMPLOYEE"
  FOR EACH ROW
  WHEN (new.id IS NULL)
    BEGIN
      SELECT TEST_EMPLOYEE_SQ.nextval INTO :new.id FROM dual;
    END;
    /
COMMIT;

C:\DjangoTraining\PK>python manage.py syncdb
Creating table test_employee
Traceback (most recent call last):
  File "manage.py", line 11, in ?
    execute_manager(settings)
  File "C:\Python24\Lib\site-packages\django\core\management.py", line
1730, in
execute_manager
    execute_from_command_line(action_mapping, argv)
  File "C:\Python24\Lib\site-packages\django\core\management.py", line
1621, in
execute_from_command_line
    action_mapping[action](int(options.verbosity),
options.interactive)
  File "C:\Python24\Lib\site-packages\django\core\management.py", line
554, in s
yncdb
    cursor.execute(statement)
  File "C:\Python24\Lib\site-packages\django\db\backends\util.py",
line 19, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\Python24\Lib\site-packages\django\db\backends\oracle
\base.py", line 1
22, in execute
    return Database.Cursor.execute(self, query, params)
cx_Oracle.DatabaseError: ORA-00904: "ID": invalid identifier


Sorry if I'm missing something really basic here but I am a bit
confused.

Thanks

Catriona



On Aug 15, 6:09 pm, Ian <[EMAIL PROTECTED]> wrote:
> Catriona,
>
> Only AutoFields are auto-incrementing in Django.  If you want to use a
> custom primary key that isn't an AutoField, you will need to populate
> the primary key yourself (or else create a custom trigger for it),
> just as you would for any other column that is both NOT NULL and
> UNIQUE.  This is true across all the backends, not just Oracle.
>
> -Ian
>
> On Aug 14, 5:34 pm, Catriona <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi Jon
>
> > Thanks for your reply. I read the article but with Oracle, a sequence
> > and trigger needs to be created for autogenerated primary keys. If I
> > try the code that you gave me, neither the sequence nor trigger are
> > created- Hide quoted text -
>
> - Show quoted text -


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to