Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed ORA-01461 error when trying to store more than 4000 bytes in a …

…TextField under Oracle

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6905 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ed5eca598e6e0fced2262dafa052d5db1661d83a 1 parent f425cb8
Ian Kelly authored December 11, 2007
19  django/db/backends/oracle/base.py
@@ -455,6 +455,23 @@ def _format_params(self, params):
455 455
         else:
456 456
             return tuple([smart_str(p, self.charset, True) for p in params])
457 457
 
  458
+    def _guess_input_sizes(self, params_list):
  459
+        # Mark any string parameter greater than 4000 characters as an NCLOB.
  460
+        if isinstance(params_list[0], dict):
  461
+            sizes = {}
  462
+            iterators = [params.iteritems() for params in params_list]
  463
+        else:
  464
+            sizes = [None] * len(params_list[0])
  465
+            iterators = [enumerate(params) for params in params_list]
  466
+        for iterator in iterators:
  467
+            for key, value in iterator:
  468
+                if isinstance(value, basestring) and len(value) > 4000:
  469
+                    sizes[key] = Database.NCLOB
  470
+        if isinstance(sizes, dict):
  471
+            self.setinputsizes(**sizes)
  472
+        else:
  473
+            self.setinputsizes(*sizes)
  474
+
458 475
     def execute(self, query, params=None):
459 476
         if params is None:
460 477
             params = []
@@ -468,6 +485,7 @@ def execute(self, query, params=None):
468 485
         if query.endswith(';') or query.endswith('/'):
469 486
             query = query[:-1]
470 487
         query = smart_str(query, self.charset) % tuple(args)
  488
+        self._guess_input_sizes([params])
471 489
         return Database.Cursor.execute(self, query, params)
472 490
 
473 491
     def executemany(self, query, params=None):
@@ -484,6 +502,7 @@ def executemany(self, query, params=None):
484 502
             query = query[:-1]
485 503
         query = smart_str(query, self.charset) % tuple(args)
486 504
         new_param_list = [self._format_params(i) for i in params]
  505
+        self._guess_input_sizes(new_param_list)
487 506
         return Database.Cursor.executemany(self, query, new_param_list)
488 507
 
489 508
     def fetchone(self):
10  tests/regressiontests/model_regress/models.py
@@ -11,6 +11,7 @@ class Article(models.Model):
11 11
     pub_date = models.DateTimeField()
12 12
     status = models.IntegerField(blank=True, null=True, choices=CHOICES)
13 13
     misc_data = models.CharField(max_length=100, blank=True)
  14
+    article_text = models.TextField()
14 15
 
15 16
     class Meta:
16 17
         ordering = ('pub_date','headline')
@@ -41,5 +42,14 @@ class Movie(models.Model):
41 42
 >>> a2 = Article.objects.get(pk=a.id)
42 43
 >>> a2.misc_data
43 44
 u''
  45
+
  46
+# TextFields can hold more than 4000 characters (this was broken in Oracle).
  47
+>>> a3 = Article(headline="Really, really big", pub_date=datetime.now())
  48
+>>> a3.article_text = "ABCDE" * 1000
  49
+>>> a3.save()
  50
+>>> a4 = Article.objects.get(pk=a3.id)
  51
+>>> len(a4.article_text)
  52
+5000
  53
+
44 54
 """
45 55
 }

0 notes on commit ed5eca5

Please sign in to comment.
Something went wrong with that request. Please try again.