diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index f67d1983356a92838cb0c6fc285d745765d639f1..282622c792e3b251c259219ef3bbf4f7852e932d 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -108,7 +108,8 @@ def persisted_environment=(environment)
     validates :ref, presence: true
 
     scope :not_interruptible, -> do
-      joins(:metadata).where.not('ci_builds_metadata.id' => Ci::BuildMetadata.scoped_build.with_interruptible.select(:id))
+      joins(:metadata)
+        .where.not(Ci::BuildMetadata.table_name => { id: Ci::BuildMetadata.scoped_build.with_interruptible.select(:id) })
     end
 
     scope :unstarted, -> { where(runner_id: nil) }
@@ -187,7 +188,7 @@ def persisted_environment=(environment)
     scope :license_management_jobs, -> { where(name: %i(license_management license_scanning)) } # handle license rename https://gitlab.com/gitlab-org/gitlab/issues/8911
 
     scope :with_secure_reports_from_config_options, -> (job_types) do
-      joins(:metadata).where("ci_builds_metadata.config_options -> 'artifacts' -> 'reports' ?| array[:job_types]", job_types: job_types)
+      joins(:metadata).where("#{Ci::BuildMetadata.quoted_table_name}.config_options -> 'artifacts' -> 'reports' ?| array[:job_types]", job_types: job_types)
     end
 
     scope :with_coverage, -> { where.not(coverage: nil) }
diff --git a/app/models/ci/build_metadata.rb b/app/models/ci/build_metadata.rb
index 6d412d490837c2c14b289aeeb161f791724805ee..33092e881f045a877b048bbf914e5da23882b070 100644
--- a/app/models/ci/build_metadata.rb
+++ b/app/models/ci/build_metadata.rb
@@ -30,7 +30,7 @@ class BuildMetadata < Ci::ApplicationRecord
 
     chronic_duration_attr_reader :timeout_human_readable, :timeout
 
-    scope :scoped_build, -> { where('ci_builds_metadata.build_id = ci_builds.id') }
+    scope :scoped_build, -> { where("#{quoted_table_name}.build_id = #{Ci::Build.quoted_table_name}.id") }
     scope :with_interruptible, -> { where(interruptible: true) }
     scope :with_exposed_artifacts, -> { where(has_exposed_artifacts: true) }
 
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 2631ed3f67255b867b2411eef6de0c82ad74ccd3..853437b2d27a37ee5ccb92cf3a4117fdefe63873 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -995,8 +995,8 @@ def environments_in_self_and_project_descendants(deployment_status: nil)
       # See: https://gitlab.com/gitlab-org/gitlab/-/issues/340781#note_699114700
       expanded_environment_names =
         builds_in_self_and_project_descendants.joins(:metadata)
-                                      .where.not('ci_builds_metadata.expanded_environment_name' => nil)
-                                      .distinct('ci_builds_metadata.expanded_environment_name')
+                                      .where.not(Ci::BuildMetadata.table_name => { expanded_environment_name: nil })
+                                      .distinct("#{Ci::BuildMetadata.quoted_table_name}.expanded_environment_name")
                                       .limit(100)
                                       .pluck(:expanded_environment_name)
 
diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb
index f6030c513364c728589ef4cb1829696c9cc0a4fe..458692ba1c0964283bed4ddd089fc7225a0a68ea 100644
--- a/spec/services/ci/create_pipeline_service_spec.rb
+++ b/spec/services/ci/create_pipeline_service_spec.rb
@@ -293,7 +293,7 @@ def execute_service(
               pipeline_on_previous_commit
                 .builds
                 .joins(:metadata)
-                .pluck(:name, 'ci_builds_metadata.interruptible')
+                .pluck(:name, "#{Ci::BuildMetadata.quoted_table_name}.interruptible")
 
             expect(interruptible_status).to contain_exactly(
               ['build_1_1', true],