diff --git a/includes/party.extender.inc b/includes/party.extender.inc
index 6d51080..0087efb 100644
--- a/includes/party.extender.inc
+++ b/includes/party.extender.inc
@@ -14,6 +14,11 @@
 class PartyQuery extends SelectQueryExtender {
 
   /**
+   * The alias of the base party table to build from. Defaults to party.
+   */
+  protected $base_alias = 'party';
+
+  /**
    * An array of table aliases for party_attached_entity keyed by join type.
    *
    * We need to store this as if we join to attached entities by differing
@@ -44,6 +49,38 @@ class PartyQuery extends SelectQueryExtender {
   protected $fields = array();
 
   /**
+   * Attempt to sniff out the party base table.
+   */
+  public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
+    parent::construct();
+
+    foreach ($this->tables as $alias => $table) {
+      if ($table['table'] == 'party') {
+        $this->base_alias = $alias;
+        break;
+      }
+    }
+  }
+
+  /**
+   * Get or set the alias for the base party table.
+   *
+   * @param string $alias
+   *   Optionally a string containing the base party table alias. If FALSE
+   *   we return the alias without changing it.
+   *
+   * @return string
+   *   The alias for the base party table.
+   */
+  public function alias($alias = FALSE) {
+    if ($alias) {
+      $this->base_alias = $alias;
+    }
+
+    return $alias;
+  }
+
+  /**
    * Create a join to an attached entity.
    *
    * @param $data_set string|array
@@ -72,7 +109,7 @@ class PartyQuery extends SelectQueryExtender {
       $this->party_attached_entity[$type] = $this->query->addJoin($type,
         'party_attached_entity',
         'pae',
-        'pae.pid = party.pid');
+        "pae.pid = {$this->base_alias}.pid");
     }
     $pae_alias = $this->party_attached_entity[$type];
 
@@ -169,7 +206,7 @@ class PartyQuery extends SelectQueryExtender {
 
     if ($data_set['set_name'] == 'party') {
       // We don't need to join as we're on party.
-      $entity_alias = 'party';
+      $entity_alias = $this->base_alias;
     }
     else {
       // Get our join to the entity table.
@@ -347,14 +384,14 @@ class PartyQuery extends SelectQueryExtender {
     $fields = array(
       'pid' => array(
         'field' => 'pid',
-        'table' => 'party',
+        'table' => $this->base_alias,
         'alias' => 'pid',
       ),
     );
 
     // Make sure we only group by party.pid.
     $group_by =& $this->query->getGroupBy();
-    $group_by = drupal_map_assoc(array('party.pid'));
+    $group_by = drupal_map_assoc(array("{$this->base_alias}.pid"));
 
     // Execute and return our pids.
     $result = $this->query->execute();
