@@ -161,8 +161,8 @@ class Service(BASE, NovaBase, models.SoftDeleteMixin):
161
161
version = sa .Column (sa .Integer , default = 0 )
162
162
163
163
instance = orm .relationship (
164
- " Instance" ,
165
- backref = 'services' ,
164
+ ' Instance' ,
165
+ back_populates = 'services' ,
166
166
primaryjoin = 'and_(Service.host == Instance.host,'
167
167
'Service.binary == "nova-compute",'
168
168
'Instance.deleted == 0)' ,
@@ -425,6 +425,96 @@ def _extra_keys(self):
425
425
426
426
hidden = sa .Column (sa .Boolean , default = False )
427
427
428
+ block_device_mapping = orm .relationship (
429
+ 'BlockDeviceMapping' ,
430
+ back_populates = 'instance' ,
431
+ primaryjoin = (
432
+ 'and_(BlockDeviceMapping.instance_uuid == Instance.uuid, '
433
+ 'BlockDeviceMapping.deleted == 0)'
434
+ ),
435
+ )
436
+ console_auth_tokens = orm .relationship (
437
+ 'ConsoleAuthToken' ,
438
+ back_populates = 'instance' ,
439
+ foreign_keys = 'ConsoleAuthToken.instance_uuid' ,
440
+ primaryjoin = (
441
+ 'and_(Instance.uuid == ConsoleAuthToken.instance_uuid,'
442
+ 'Instance.deleted == 0)'
443
+ ),
444
+ )
445
+ extra = orm .relationship (
446
+ 'InstanceExtra' ,
447
+ back_populates = 'instance' ,
448
+ uselist = False ,
449
+ )
450
+ info_cache = orm .relationship (
451
+ 'InstanceInfoCache' ,
452
+ back_populates = 'instance' ,
453
+ uselist = False ,
454
+ )
455
+ pci_devices = orm .relationship (
456
+ 'PciDevice' ,
457
+ back_populates = 'instance' ,
458
+ foreign_keys = 'PciDevice.instance_uuid' ,
459
+ primaryjoin = (
460
+ 'and_(Instance.uuid == PciDevice.instance_uuid,'
461
+ 'PciDevice.deleted == 0)'
462
+ ),
463
+ )
464
+ services = orm .relationship (
465
+ 'Service' ,
466
+ back_populates = 'instance' ,
467
+ primaryjoin = (
468
+ 'and_(Instance.host == Service.host,'
469
+ 'Service.binary == "nova-compute",'
470
+ 'Instance.deleted == 0)'
471
+ ),
472
+ foreign_keys = 'Service.host' ,
473
+ )
474
+ security_groups = orm .relationship (
475
+ 'SecurityGroup' ,
476
+ secondary = 'security_group_instance_association' ,
477
+ back_populates = 'instances' ,
478
+ primaryjoin = (
479
+ 'and_('
480
+ 'SecurityGroupInstanceAssociation.instance_uuid == Instance.uuid,'
481
+ # (anthony) the condition below shouldn't be necessary now that the
482
+ # association is being marked as deleted. However, removing this
483
+ # may cause existing deployments to choke, so I'm leaving it
484
+ 'Instance.deleted == 0)'
485
+ ),
486
+ secondaryjoin = (
487
+ 'and_('
488
+ 'SecurityGroup.id == SecurityGroupInstanceAssociation.security_group_id,' # noqa: E501
489
+ 'SecurityGroupInstanceAssociation.deleted == 0,'
490
+ 'SecurityGroup.deleted == 0)'
491
+ ),
492
+ )
493
+ system_metadata = orm .relationship (
494
+ 'InstanceSystemMetadata' ,
495
+ back_populates = 'instance' ,
496
+ )
497
+ tags = orm .relationship (
498
+ 'Tag' ,
499
+ back_populates = 'instance' ,
500
+ primaryjoin = (
501
+ 'and_(Instance.uuid == Tag.resource_id,Instance.deleted == 0)'
502
+ ),
503
+ foreign_keys = 'Tag.resource_id' ,
504
+ )
505
+
506
+
507
+ # NOTE(stephenfin): https://github.com/sqlalchemy/sqlalchemy/discussions/8619
508
+ Instance .metadata = orm .relationship (
509
+ 'InstanceMetadata' ,
510
+ back_populates = 'instance' ,
511
+ foreign_keys = 'InstanceMetadata.instance_uuid' ,
512
+ primaryjoin = (
513
+ 'and_(Instance.uuid == InstanceMetadata.instance_uuid,'
514
+ 'InstanceMetadata.deleted == 0)'
515
+ ),
516
+ )
517
+
428
518
429
519
class InstanceInfoCache (BASE , NovaBase , models .SoftDeleteMixin ):
430
520
"""Represents a cache of information about an instance
@@ -442,7 +532,7 @@ class InstanceInfoCache(BASE, NovaBase, models.SoftDeleteMixin):
442
532
instance_uuid = sa .Column (sa .String (36 ), sa .ForeignKey ('instances.uuid' ),
443
533
nullable = False )
444
534
instance = orm .relationship (Instance ,
445
- backref = orm . backref ( 'info_cache' , uselist = False ) ,
535
+ back_populates = 'info_cache' ,
446
536
foreign_keys = instance_uuid ,
447
537
primaryjoin = instance_uuid == Instance .uuid )
448
538
@@ -466,8 +556,7 @@ class InstanceExtra(BASE, NovaBase, models.SoftDeleteMixin):
466
556
# and can be removed in the future release.
467
557
resources = orm .deferred (sa .Column (sa .Text ))
468
558
instance = orm .relationship (Instance ,
469
- backref = orm .backref ('extra' ,
470
- uselist = False ),
559
+ back_populates = 'extra' ,
471
560
foreign_keys = instance_uuid ,
472
561
primaryjoin = instance_uuid == Instance .uuid )
473
562
@@ -616,7 +705,7 @@ class BlockDeviceMapping(BASE, NovaBase, models.SoftDeleteMixin):
616
705
# transition period first.
617
706
uuid = sa .Column (sa .String (36 ))
618
707
instance = orm .relationship (Instance ,
619
- backref = orm . backref ( 'block_device_mapping' ) ,
708
+ back_populates = 'block_device_mapping' ,
620
709
foreign_keys = instance_uuid ,
621
710
primaryjoin = 'and_(BlockDeviceMapping.'
622
711
'instance_uuid=='
@@ -664,10 +753,9 @@ class BlockDeviceMapping(BASE, NovaBase, models.SoftDeleteMixin):
664
753
encryption_format = sa .Column (sa .String (128 ))
665
754
encryption_options = sa .Column (sa .String (4096 ))
666
755
756
+
667
757
# TODO(stephenfin): Remove once we drop the security_groups field from the
668
758
# Instance table. Until then, this is tied to the SecurityGroup table
669
-
670
-
671
759
class SecurityGroupInstanceAssociation (BASE , NovaBase , models .SoftDeleteMixin ):
672
760
__tablename__ = 'security_group_instance_association'
673
761
__table_args__ = (
@@ -698,6 +786,7 @@ class SecurityGroup(BASE, NovaBase, models.SoftDeleteMixin):
698
786
project_id = sa .Column (sa .String (255 ))
699
787
700
788
instances = orm .relationship (Instance ,
789
+ back_populates = 'security_groups' ,
701
790
secondary = "security_group_instance_association" ,
702
791
primaryjoin = 'and_('
703
792
'SecurityGroup.id == '
@@ -709,8 +798,17 @@ class SecurityGroup(BASE, NovaBase, models.SoftDeleteMixin):
709
798
# (anthony) the condition below shouldn't be necessary now that the
710
799
# association is being marked as deleted. However, removing this
711
800
# may cause existing deployments to choke, so I'm leaving it
712
- 'Instance.deleted == 0)' ,
713
- backref = 'security_groups' )
801
+ 'Instance.deleted == 0)' )
802
+
803
+ rules = orm .relationship (
804
+ 'SecurityGroupIngressRule' ,
805
+ primaryjoin = (
806
+ 'and_('
807
+ 'SecurityGroupIngressRule.parent_group_id == SecurityGroup.id,'
808
+ 'SecurityGroupIngressRule.deleted == 0)'
809
+ ),
810
+ back_populates = 'parent_group' ,
811
+ )
714
812
715
813
716
814
# TODO(stephenfin): Remove once we drop the security_groups field from the
@@ -723,8 +821,9 @@ class SecurityGroupIngressRule(BASE, NovaBase, models.SoftDeleteMixin):
723
821
724
822
parent_group_id = sa .Column (
725
823
sa .Integer , sa .ForeignKey ('security_groups.id' ))
726
- parent_group = orm .relationship ("SecurityGroup" , backref = "rules" ,
727
- foreign_keys = parent_group_id ,
824
+ parent_group = orm .relationship ("SecurityGroup" ,
825
+ back_populates = "rules" ,
826
+ foreign_keys = [parent_group_id ],
728
827
primaryjoin = 'and_('
729
828
'SecurityGroupIngressRule.parent_group_id == SecurityGroup.id,'
730
829
'SecurityGroupIngressRule.deleted == 0)' )
@@ -738,7 +837,7 @@ class SecurityGroupIngressRule(BASE, NovaBase, models.SoftDeleteMixin):
738
837
# granting access for.
739
838
group_id = sa .Column (sa .Integer , sa .ForeignKey ('security_groups.id' ))
740
839
grantee_group = orm .relationship ("SecurityGroup" ,
741
- foreign_keys = group_id ,
840
+ foreign_keys = [ group_id ] ,
742
841
primaryjoin = 'and_('
743
842
'SecurityGroupIngressRule.group_id == SecurityGroup.id,'
744
843
'SecurityGroupIngressRule.deleted == 0)' )
@@ -824,12 +923,16 @@ class InstanceMetadata(BASE, NovaBase, models.SoftDeleteMixin):
824
923
key = sa .Column (sa .String (255 ))
825
924
value = sa .Column (sa .String (255 ))
826
925
instance_uuid = sa .Column (sa .String (36 ), sa .ForeignKey ('instances.uuid' ))
827
- instance = orm .relationship (Instance , backref = "metadata" ,
828
- foreign_keys = instance_uuid ,
829
- primaryjoin = 'and_('
830
- 'InstanceMetadata.instance_uuid == '
831
- 'Instance.uuid,'
832
- 'InstanceMetadata.deleted == 0)' )
926
+
927
+ instance = orm .relationship (
928
+ Instance ,
929
+ back_populates = "metadata" ,
930
+ foreign_keys = instance_uuid ,
931
+ primaryjoin = (
932
+ 'and_(InstanceMetadata.instance_uuid == Instance.uuid,'
933
+ 'InstanceMetadata.deleted == 0)'
934
+ ),
935
+ )
833
936
834
937
835
938
class InstanceSystemMetadata (BASE , NovaBase , models .SoftDeleteMixin ):
@@ -845,8 +948,11 @@ class InstanceSystemMetadata(BASE, NovaBase, models.SoftDeleteMixin):
845
948
sa .ForeignKey ('instances.uuid' ),
846
949
nullable = False )
847
950
848
- instance = orm .relationship (Instance , backref = "system_metadata" ,
849
- foreign_keys = instance_uuid )
951
+ instance = orm .relationship (
952
+ Instance ,
953
+ back_populates = 'system_metadata' ,
954
+ foreign_keys = instance_uuid ,
955
+ )
850
956
851
957
852
958
class VolumeUsage (BASE , NovaBase , models .SoftDeleteMixin ):
@@ -1022,7 +1128,8 @@ class PciDevice(BASE, NovaBase, models.SoftDeleteMixin):
1022
1128
numa_node = sa .Column (sa .Integer , nullable = True )
1023
1129
1024
1130
parent_addr = sa .Column (sa .String (12 ), nullable = True )
1025
- instance = orm .relationship (Instance , backref = "pci_devices" ,
1131
+ instance = orm .relationship (Instance ,
1132
+ back_populates = "pci_devices" ,
1026
1133
foreign_keys = instance_uuid ,
1027
1134
primaryjoin = 'and_('
1028
1135
'PciDevice.instance_uuid == Instance.uuid,'
@@ -1040,11 +1147,11 @@ class Tag(BASE, models.ModelBase):
1040
1147
tag = sa .Column (sa .Unicode (80 ), primary_key = True , nullable = False )
1041
1148
1042
1149
instance = orm .relationship (
1043
- "Instance" ,
1044
- backref = 'tags' ,
1150
+ 'Instance' ,
1151
+ back_populates = 'tags' ,
1152
+ foreign_keys = resource_id ,
1045
1153
primaryjoin = 'and_(Tag.resource_id == Instance.uuid,'
1046
1154
'Instance.deleted == 0)' ,
1047
- foreign_keys = resource_id
1048
1155
)
1049
1156
1050
1157
@@ -1074,7 +1181,7 @@ class ConsoleAuthToken(BASE, NovaBase):
1074
1181
1075
1182
instance = orm .relationship (
1076
1183
"Instance" ,
1077
- backref = 'console_auth_tokens' ,
1184
+ back_populates = 'console_auth_tokens' ,
1078
1185
primaryjoin = 'and_(ConsoleAuthToken.instance_uuid == Instance.uuid,'
1079
1186
'Instance.deleted == 0)' ,
1080
1187
foreign_keys = instance_uuid
0 commit comments