2009-03-09 11:35:02 +08:00
|
|
|
"""
|
|
|
|
Models can have a ``managed`` attribute, which specifies whether the SQL code
|
|
|
|
is generated for the table on various manage.py operations.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2011-08-12 22:14:15 +08:00
|
|
|
# All of these models are created in the database by Django.
|
2009-03-09 11:35:02 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-09 11:35:02 +08:00
|
|
|
class A01(models.Model):
|
|
|
|
f_a = models.CharField(max_length=10, db_index=True)
|
|
|
|
f_b = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
2011-09-11 04:06:10 +08:00
|
|
|
db_table = 'a01'
|
2009-03-09 11:35:02 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-03-09 11:35:02 +08:00
|
|
|
return self.f_a
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-09 11:35:02 +08:00
|
|
|
class B01(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
fk_a = models.ForeignKey(A01, models.CASCADE)
|
2009-03-09 11:35:02 +08:00
|
|
|
f_a = models.CharField(max_length=10, db_index=True)
|
|
|
|
f_b = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
2011-09-11 04:06:10 +08:00
|
|
|
db_table = 'b01'
|
2009-03-09 11:35:02 +08:00
|
|
|
# 'managed' is True by default. This tests we can set it explicitly.
|
|
|
|
managed = True
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-03-09 11:35:02 +08:00
|
|
|
return self.f_a
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-09 11:35:02 +08:00
|
|
|
class C01(models.Model):
|
2011-09-11 04:06:10 +08:00
|
|
|
mm_a = models.ManyToManyField(A01, db_table='d01')
|
2009-03-09 11:35:02 +08:00
|
|
|
f_a = models.CharField(max_length=10, db_index=True)
|
|
|
|
f_b = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
2011-09-11 04:06:10 +08:00
|
|
|
db_table = 'c01'
|
2009-03-09 11:35:02 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-03-09 11:35:02 +08:00
|
|
|
return self.f_a
|
|
|
|
|
|
|
|
# All of these models use the same tables as the previous set (they are shadows
|
|
|
|
# of possibly a subset of the columns). There should be no creation errors,
|
|
|
|
# since we have told Django they aren't managed by Django.
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-09 11:35:02 +08:00
|
|
|
class A02(models.Model):
|
|
|
|
f_a = models.CharField(max_length=10, db_index=True)
|
|
|
|
|
|
|
|
class Meta:
|
2011-09-11 04:06:10 +08:00
|
|
|
db_table = 'a01'
|
2009-03-09 11:35:02 +08:00
|
|
|
managed = False
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-03-09 11:35:02 +08:00
|
|
|
return self.f_a
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-09 11:35:02 +08:00
|
|
|
class B02(models.Model):
|
|
|
|
class Meta:
|
2011-09-11 04:06:10 +08:00
|
|
|
db_table = 'b01'
|
2009-03-09 11:35:02 +08:00
|
|
|
managed = False
|
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
fk_a = models.ForeignKey(A02, models.CASCADE)
|
2009-03-09 11:35:02 +08:00
|
|
|
f_a = models.CharField(max_length=10, db_index=True)
|
|
|
|
f_b = models.IntegerField()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-03-09 11:35:02 +08:00
|
|
|
return self.f_a
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-09 11:35:02 +08:00
|
|
|
# To re-use the many-to-many intermediate table, we need to manually set up
|
|
|
|
# things up.
|
|
|
|
class C02(models.Model):
|
|
|
|
mm_a = models.ManyToManyField(A02, through="Intermediate")
|
|
|
|
f_a = models.CharField(max_length=10, db_index=True)
|
|
|
|
f_b = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
2011-09-11 04:06:10 +08:00
|
|
|
db_table = 'c01'
|
2009-03-09 11:35:02 +08:00
|
|
|
managed = False
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-03-09 11:35:02 +08:00
|
|
|
return self.f_a
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-09 11:35:02 +08:00
|
|
|
class Intermediate(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
a02 = models.ForeignKey(A02, models.CASCADE, db_column="a01_id")
|
|
|
|
c02 = models.ForeignKey(C02, models.CASCADE, db_column="c01_id")
|
2009-03-09 11:35:02 +08:00
|
|
|
|
|
|
|
class Meta:
|
2011-09-11 04:06:10 +08:00
|
|
|
db_table = 'd01'
|
2009-03-09 11:35:02 +08:00
|
|
|
managed = False
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-04-09 23:03:31 +08:00
|
|
|
# These next models test the creation (or not) of many to many join tables
|
2009-04-11 10:12:31 +08:00
|
|
|
# between managed and unmanaged models. A join table between two unmanaged
|
|
|
|
# models shouldn't be automatically created (see #10647).
|
2009-04-09 23:03:31 +08:00
|
|
|
#
|
2009-04-11 10:12:31 +08:00
|
|
|
|
|
|
|
# Firstly, we need some models that will create the tables, purely so that the
|
|
|
|
# tables are created. This is a test setup, not a requirement for unmanaged
|
|
|
|
# models.
|
|
|
|
class Proxy1(models.Model):
|
|
|
|
class Meta:
|
|
|
|
db_table = "unmanaged_models_proxy1"
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-04-11 10:12:31 +08:00
|
|
|
class Proxy2(models.Model):
|
|
|
|
class Meta:
|
|
|
|
db_table = "unmanaged_models_proxy2"
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-04-11 10:12:31 +08:00
|
|
|
class Unmanaged1(models.Model):
|
2009-04-09 23:03:31 +08:00
|
|
|
class Meta:
|
|
|
|
managed = False
|
2009-04-11 10:12:31 +08:00
|
|
|
db_table = "unmanaged_models_proxy1"
|
2009-04-09 23:03:31 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2015-12-03 07:55:50 +08:00
|
|
|
# Unmanaged with an m2m to unmanaged: the intermediary table won't be created.
|
2009-04-09 23:03:31 +08:00
|
|
|
class Unmanaged2(models.Model):
|
|
|
|
mm = models.ManyToManyField(Unmanaged1)
|
2009-04-11 10:12:31 +08:00
|
|
|
|
2009-04-09 23:03:31 +08:00
|
|
|
class Meta:
|
|
|
|
managed = False
|
2009-04-11 10:12:31 +08:00
|
|
|
db_table = "unmanaged_models_proxy2"
|
2009-04-09 23:03:31 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-04-09 23:03:31 +08:00
|
|
|
# Here's an unmanaged model with an m2m to a managed one; the intermediary
|
|
|
|
# table *will* be created (unless given a custom `through` as for C02 above).
|
|
|
|
class Managed1(models.Model):
|
|
|
|
mm = models.ManyToManyField(Unmanaged1)
|