2929if django_version >= (4 , 0 ):
3030 from django .db .models .sql import Query
3131 from django .db .backends .ddl_references import Expressions
32-
33-
32+ # Import CompositePrimaryKey only if Django version is 5.2 or higher
33+ if django_version >= (5 , 2 ):
34+ from django .db .models .fields .composite import CompositePrimaryKey
3435class Statement (DjStatement ):
3536 def __hash__ (self ):
3637 return hash ((self .template , str (self .parts ['name' ])))
@@ -1315,7 +1316,19 @@ def create_model(self, model):
13151316 autoinc_sql = self .connection .ops .autoinc_sql (model ._meta .db_table , field .column )
13161317 if autoinc_sql :
13171318 self .deferred_sql .extend (autoinc_sql )
1318-
1319+
1320+ # Initialize composite_pk_sql to None; will be set if composite primary key is detected
1321+ composite_pk_sql = None
1322+ # Check if Django version is >= 5.2 and the model has composite primary key fields
1323+ if django_version >= (5 , 2 ) and hasattr (model ._meta , "pk_fields" ):
1324+ #specifically refers to the primary key field of that model.
1325+ pk = model ._meta .pk
1326+ # Check if the primary key is a CompositePrimaryKey instance
1327+ if isinstance (pk , CompositePrimaryKey ):
1328+ # Get the column names for all fields in the composite primary key
1329+ pk_columns = [field .column for field in model ._meta .pk_fields ]
1330+ # Build the PRIMARY KEY SQL clause for the composite key
1331+ composite_pk_sql = "PRIMARY KEY (%s)" % ", " .join (self .quote_name (col ) for col in pk_columns )
13191332 # Add any unique_togethers (always deferred, as some fields might be
13201333 # created afterwards, like geometry fields with some backends)
13211334 for field_names in model ._meta .unique_together :
@@ -1328,6 +1341,9 @@ def create_model(self, model):
13281341 self .deferred_sql .append (self ._create_unique_sql (model , columns , condition = condition ))
13291342
13301343 constraints = [constraint .constraint_sql (model , self ) for constraint in model ._meta .constraints ]
1344+ # If a composite primary key SQL clause was generated, insert it at the beginning of the constraints list
1345+ if composite_pk_sql :
1346+ constraints .insert (0 , composite_pk_sql )
13311347 # Make the table
13321348 sql = self .sql_create_table % {
13331349 "table" : self .quote_name (model ._meta .db_table ),
0 commit comments