at method

S at({
  1. String? schema,
  2. String? table,
})

This query over another schema or table name.

What a tenant-per-schema deployment needs: the same generated types pointed at tenant2.Orders. The binding is rebound and the fields resolve against the new source, so a predicate written after this call compiles to [tenant2].[Orders].[Col] rather than to the table the code was generated from.

Relation targets are not moved. A foreign key names one table, and assuming every related table lives in the same tenant schema would be a guess about the deployment; call at on the related query too, or generate against the schema you mean.

Refused once the query carries predicates, ordering or includes: those were built from the old source and would silently name a table this statement does not have. Rebind first, then narrow.

Implementation

S at({String? schema, String? table}) {
  if (schema == null && table == null) return _self;
  if (state.where.isNotEmpty ||
      state.orderBy.isNotEmpty ||
      state.ctes.isNotEmpty ||
      included.isNotEmpty) {
    throw StateError(
      'at(schema:, table:) has to come before where / orderBy / include '
      'on ${binding.qualifiedName}. Those were built from this table\'s '
      'columns and would still name it after the rebind, so the '
      'statement would refer to a source it does not have. Start the '
      'chain with at(...).',
    );
  }
  final rebound = binding.forTable(schema: schema, table: table);
  return recreateAt(
    MssqlQueryContext<TRow>(
      session: context.session,
      binding: rebound,
      dialect: context.fixedDialect,
      clock: context.clock,
      changes: context.changes,
      capabilities: context.capabilities,
      observer: context.observer,
      observerOptions: context.observerOptions,
    ),
    state,
    included,
  );
}