mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #13774 -- Added models.Field.rel_db_type().
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							0e7d59df3e
						
					
				
				
					commit
					b61eab18f7
				
			| @@ -626,6 +626,14 @@ class Field(RegisterLookupMixin): | ||||
|         except KeyError: | ||||
|             return None | ||||
|  | ||||
|     def rel_db_type(self, connection): | ||||
|         """ | ||||
|         Return the data type that a related field pointing to this field should | ||||
|         use. For example, this method is called by ForeignKey and OneToOneField | ||||
|         to determine its data type. | ||||
|         """ | ||||
|         return self.db_type(connection) | ||||
|  | ||||
|     def db_parameters(self, connection): | ||||
|         """ | ||||
|         Extension of db_type(), providing a range of different return | ||||
| @@ -960,6 +968,9 @@ class AutoField(Field): | ||||
|                 params={'value': value}, | ||||
|             ) | ||||
|  | ||||
|     def rel_db_type(self, connection): | ||||
|         return IntegerField().db_type(connection=connection) | ||||
|  | ||||
|     def validate(self, value, model_instance): | ||||
|         pass | ||||
|  | ||||
| @@ -2072,7 +2083,24 @@ class NullBooleanField(Field): | ||||
|         return super(NullBooleanField, self).formfield(**defaults) | ||||
|  | ||||
|  | ||||
| class PositiveIntegerField(IntegerField): | ||||
| class PositiveIntegerRelDbTypeMixin(object): | ||||
|  | ||||
|     def rel_db_type(self, connection): | ||||
|         """ | ||||
|         Return the data type that a related field pointing to this field should | ||||
|         use. In most cases, a foreign key pointing to a positive integer | ||||
|         primary key will have an integer column data type but some databases | ||||
|         (e.g. MySQL) have an unsigned integer type. In that case | ||||
|         (related_fields_match_type=True), the primary key should return its | ||||
|         db_type. | ||||
|         """ | ||||
|         if connection.features.related_fields_match_type: | ||||
|             return self.db_type(connection) | ||||
|         else: | ||||
|             return IntegerField().db_type(connection=connection) | ||||
|  | ||||
|  | ||||
| class PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField): | ||||
|     description = _("Positive integer") | ||||
|  | ||||
|     def get_internal_type(self): | ||||
| @@ -2084,7 +2112,7 @@ class PositiveIntegerField(IntegerField): | ||||
|         return super(PositiveIntegerField, self).formfield(**defaults) | ||||
|  | ||||
|  | ||||
| class PositiveSmallIntegerField(IntegerField): | ||||
| class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField): | ||||
|     description = _("Positive small integer") | ||||
|  | ||||
|     def get_internal_type(self): | ||||
|   | ||||
| @@ -18,10 +18,7 @@ from django.utils.functional import cached_property, curry | ||||
| from django.utils.translation import ugettext_lazy as _ | ||||
| from django.utils.version import get_docs_version | ||||
|  | ||||
| from . import ( | ||||
|     AutoField, Field, IntegerField, PositiveIntegerField, | ||||
|     PositiveSmallIntegerField, | ||||
| ) | ||||
| from . import Field | ||||
| from .related_descriptors import ( | ||||
|     ForwardManyToOneDescriptor, ManyToManyDescriptor, | ||||
|     ReverseManyToOneDescriptor, ReverseOneToOneDescriptor, | ||||
| @@ -935,19 +932,7 @@ class ForeignKey(ForeignObject): | ||||
|         return super(ForeignKey, self).formfield(**defaults) | ||||
|  | ||||
|     def db_type(self, connection): | ||||
|         # The database column type of a ForeignKey is the column type | ||||
|         # of the field to which it points. An exception is if the ForeignKey | ||||
|         # points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField, | ||||
|         # in which case the column type is simply that of an IntegerField. | ||||
|         # If the database needs similar types for key fields however, the only | ||||
|         # thing we can do is making AutoField an IntegerField. | ||||
|         rel_field = self.target_field | ||||
|         if (isinstance(rel_field, AutoField) or | ||||
|                 (not connection.features.related_fields_match_type and | ||||
|                 isinstance(rel_field, (PositiveIntegerField, | ||||
|                                        PositiveSmallIntegerField)))): | ||||
|             return IntegerField().db_type(connection=connection) | ||||
|         return rel_field.db_type(connection=connection) | ||||
|         return self.target_field.rel_db_type(connection=connection) | ||||
|  | ||||
|     def db_parameters(self, connection): | ||||
|         return {"type": self.db_type(connection), "check": []} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user