1

I have a model MyModel in django one of whose fields gets a value from a sequence upon insert into Oracle 12c. So I do

myModel = MyModel(field1=123,field2='abc')
myModel.save()

The problem is that field1 gets its value upon insert into the Oracle database. If I omit the field then try to save I get ORA-01400: cannot insert NULL. There's no way I can give the field a value in django, either. How do I save the model? Thanks.

r_zelazny
  • 517
  • 3
  • 19

1 Answers1

1

Try changing your field1 to an AutoField, so every time you save a new object Oracle will assign the consecutive value.

field1 = models.AutoField()

If this is your primary key then pass the primary_key=True argument to the AutoField constructor.

UPDATE

When using legacy databases, you have to remove the mapped fields that are sequences in the database, so Django does the work of finding the way to work with those fields.

Juan D. Gómez
  • 479
  • 1
  • 7
  • 21
  • Thanks for your help so far. After changing my model to use Autofield still having problems. When I create an instance of myModel, I tried omitting field1 thus: myModel = Model(field2='abc') but am still getting the ORA-01400: cannot insert NULL error. – r_zelazny Feb 01 '17 at 15:09
  • Is your field an ID?, I mean, if it is, just remove it from the model. See this: http://stackoverflow.com/a/41591660/6855686 – Juan D. Gómez Feb 01 '17 at 16:10
  • Brilliant. Thanks for your help! – r_zelazny Feb 01 '17 at 16:31
  • I am glad to help, Please upvote my answer, you know... It helps. – Juan D. Gómez Feb 01 '17 at 16:33