| 1 |
""" |
| 2 |
Models can have a ``managed`` attribute, which specifies whether the SQL code |
| 3 |
is generated for the table on various manage.py operations. |
| 4 |
""" |
| 5 |
|
| 6 |
from django.db import models |
| 7 |
|
| 8 |
# All of these models are created in the database by Django. |
| 9 |
|
| 10 |
|
| 11 |
class A01(models.Model): |
| 12 |
f_a = models.CharField(max_length=10, db_index=True) |
| 13 |
f_b = models.IntegerField() |
| 14 |
|
| 15 |
class Meta: |
| 16 |
db_table = "a01" |
| 17 |
|
| 18 |
|
| 19 |
class B01(models.Model): |
| 20 |
fk_a = models.ForeignKey(A01, models.CASCADE) |
| 21 |
f_a = models.CharField(max_length=10, db_index=True) |
| 22 |
f_b = models.IntegerField() |
| 23 |
|
| 24 |
class Meta: |
| 25 |
db_table = "b01" |
| 26 |
# 'managed' is True by default. This tests we can set it explicitly. |
| 27 |
managed = True |
| 28 |
|
| 29 |
|
| 30 |
class C01(models.Model): |
| 31 |
mm_a = models.ManyToManyField(A01, db_table="d01") |
| 32 |
f_a = models.CharField(max_length=10, db_index=True) |
| 33 |
f_b = models.IntegerField() |
| 34 |
|
| 35 |
class Meta: |
| 36 |
db_table = "c01" |
| 37 |
|
| 38 |
|
| 39 |
# All of these models use the same tables as the previous set (they are shadows |
| 40 |
# of possibly a subset of the columns). There should be no creation errors, |
| 41 |
# since we have told Django they aren't managed by Django. |
| 42 |
|
| 43 |
|
| 44 |
class A02(models.Model): |
| 45 |
f_a = models.CharField(max_length=10, db_index=True) |
| 46 |
|
| 47 |
class Meta: |
| 48 |
db_table = "a01" |
| 49 |
managed = False |
| 50 |
|
| 51 |
|
| 52 |
class B02(models.Model): |
| 53 |
class Meta: |
| 54 |
db_table = "b01" |
| 55 |
managed = False |
| 56 |
|
| 57 |
fk_a = models.ForeignKey(A02, models.CASCADE) |
| 58 |
f_a = models.CharField(max_length=10, db_index=True) |
| 59 |
f_b = models.IntegerField() |
| 60 |
|
| 61 |
|
| 62 |
# To re-use the many-to-many intermediate table, we need to manually set up |
| 63 |
# things up. |
| 64 |
class C02(models.Model): |
| 65 |
mm_a = models.ManyToManyField(A02, through="Intermediate") |
| 66 |
f_a = models.CharField(max_length=10, db_index=True) |
| 67 |
f_b = models.IntegerField() |
| 68 |
|
| 69 |
class Meta: |
| 70 |
db_table = "c01" |
| 71 |
managed = False |
| 72 |
|
| 73 |
|
| 74 |
class Intermediate(models.Model): |
| 75 |
a02 = models.ForeignKey(A02, models.CASCADE, db_column="a01_id") |
| 76 |
c02 = models.ForeignKey(C02, models.CASCADE, db_column="c01_id") |
| 77 |
|
| 78 |
class Meta: |
| 79 |
db_table = "d01" |
| 80 |
managed = False |
| 81 |
|
| 82 |
|
| 83 |
# These next models test the creation (or not) of many to many join tables |
| 84 |
# between managed and unmanaged models. A join table between two unmanaged |
| 85 |
# models shouldn't be automatically created (see #10647). |
| 86 |
# |
| 87 |
|
| 88 |
|
| 89 |
# Firstly, we need some models that will create the tables, purely so that the |
| 90 |
# tables are created. This is a test setup, not a requirement for unmanaged |
| 91 |
# models. |
| 92 |
class Proxy1(models.Model): |
| 93 |
class Meta: |
| 94 |
db_table = "unmanaged_models_proxy1" |
| 95 |
|
| 96 |
|
| 97 |
class Proxy2(models.Model): |
| 98 |
class Meta: |
| 99 |
db_table = "unmanaged_models_proxy2" |
| 100 |
|
| 101 |
|
| 102 |
class Unmanaged1(models.Model): |
| 103 |
class Meta: |
| 104 |
managed = False |
| 105 |
db_table = "unmanaged_models_proxy1" |
| 106 |
|
| 107 |
|
| 108 |
# Unmanaged with an m2m to unmanaged: the intermediary table won't be created. |
| 109 |
class Unmanaged2(models.Model): |
| 110 |
mm = models.ManyToManyField(Unmanaged1) |
| 111 |
|
| 112 |
class Meta: |
| 113 |
managed = False |
| 114 |
db_table = "unmanaged_models_proxy2" |
| 115 |
|
| 116 |
|
| 117 |
# Here's an unmanaged model with an m2m to a managed one; the intermediary |
| 118 |
# table *will* be created (unless given a custom `through` as for C02 above). |
| 119 |
class Managed1(models.Model): |
| 120 |
mm = models.ManyToManyField(Unmanaged1) |