diff options
| author | Jason Pellerin <jpellerin@gmail.com> | 2006-07-03 20:56:24 +0000 |
|---|---|---|
| committer | Jason Pellerin <jpellerin@gmail.com> | 2006-07-03 20:56:24 +0000 |
| commit | f194f74aa62727e4624b3e6e1e06fdbeebc4f3b5 (patch) | |
| tree | 46181d8ba337e8a773486755d582ae35857879d5 /tests | |
| parent | 54d611d2a2e65419983fa1d63cc435dddd04acb3 (diff) | |
[multi-db] Added install() and other schema manipulation methods to Manager. Fixed bug in manager assignment for inherited classes (objects and _default_manager in child class were still those belonging to parent class).
git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3266 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/othertests/manager_schema_manipulation.py | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/tests/othertests/manager_schema_manipulation.py b/tests/othertests/manager_schema_manipulation.py new file mode 100644 index 0000000000..e3c8fb37b9 --- /dev/null +++ b/tests/othertests/manager_schema_manipulation.py @@ -0,0 +1,131 @@ +""" +# Django uses a model's default manager to perform schema manipulations such as +# creating or dropping the model's table. + +>>> from django.db import models + +# default connection +>>> class DA(models.Model): +... name = models.CharField(maxlength=20) +... +... def __str__(self): +... return self.name + +# connection django_test_db_a +>>> class PA(models.Model): +... name = models.CharField(maxlength=20) +... # This creates a cycle in the dependency graph +... c = models.ForeignKey('PC', null=True) +... +... def __str__(self): +... return self.name +... +... class Meta: +... db_connection = 'django_test_db_a' + +>>> class PB(models.Model): +... name = models.CharField(maxlength=20) +... a = models.ForeignKey(PA) +... +... def __str__(self): +... return self.name +... +... class Meta: +... db_connection = 'django_test_db_a' + +>>> class PC(models.Model): +... name = models.CharField(maxlength=20) +... b = models.ForeignKey(PB) +... +... def __str__(self): +... return self.name +... +... class Meta: +... db_connection = 'django_test_db_a' + +# connection django_test_db_b +>>> class QA(models.Model): +... name = models.CharField(maxlength=20) +... +... def __str__(self): +... return self.name +... +... class Meta: +... db_connection = 'django_test_db_b' + +>>> class QB(models.Model): +... name = models.CharField(maxlength=20) +... a = models.ForeignKey(QA) +... +... def __str__(self): +... return self.name +... +... class Meta: +... db_connection = 'django_test_db_b' + +# many-many +>>> class QC(models.Model): +... name = models.CharField(maxlength=20) +... +... def __str__(self): +... return self.name +... +... class Meta: +... db_connection = 'django_test_db_b' + +>>> class QD(models.Model): +... name = models.CharField(maxlength=20) +... qcs = models.ManyToManyField(QC) +... +... def __str__(self): +... return self.name +... +... class Meta: +... db_connection = 'django_test_db_b' + +# Using the manager, models can be installed individually, whether they +# use the default connection or a named connection. + +>>> DA.objects.install() +[] +>>> QA.objects.install() +[] +>>> QB.objects.install() +[] +>>> DA.objects.all() +[] +>>> list(QA.objects.all()) +[] +>>> list(QB.objects.all()) +[] +>>> QA(name="something").save() +>>> QA.objects.all() +[<QA: something>] + +# The `install()` method returns a tuple, the first element of which is a +# list of statements that were executed, and the second, pending +# statements that could not be executed because (for instance) they are +# meant to establish foreign key relationships to tables that don't +# exist. These are bound to the model's connection and should +# be executed after all models in the app have been installed. + +# NOTE: pretend db supports constraints for this test +>>> real_cnst = PA._meta.connection_info.backend.supports_constraints +>>> PA._meta.connection_info.backend.supports_constraints = True +>>> result = PA.objects.install() +>>> result +[BoundStatement('ALTER TABLE "othertests_pa" ADD CONSTRAINT "c_id_referencing_othertests_pc_id" FOREIGN KEY ("c_id") REFERENCES "othertests_pc" ("id");')] + +# NOTE: restore real constraint flag +>>> PA._meta.connection_info.backend.supports_constraints = real_cnst + +# Models with many-many relationships will also have pending statement +# lists. Like other pending statements, these should be executed after +# all models in the app have been installed. + +>>> QC.objects.install() +[] +>>> QD.objects.install() +[BoundStatement('CREATE TABLE "othertests_qd_qcs" (...);')] + +""" |
