isSameAs method

bool isSameAs(
  1. Model<Model>? other
)
inherited

Determine if two models represent the same database record.

Compares:

  • Primary key values
  • Table names
  • Connection names (if available)

Example:

final user1 = await User.query().find(1);
final user2 = await User.query().find(1);
print(user1.isSameAs(user2)); // true

final user3 = await User.query().find(2);
print(user1.isSameAs(user3)); // false

Implementation

bool isSameAs(Model? other) {
  if (other == null) return false;

  final def1 = expectDefinition();
  final def2 = (other as Model<dynamic>).expectDefinition();

  // Compare primary keys
  final pk1 = _primaryKeyValue(def1);
  final pk2 = (other as Model<dynamic>)._primaryKeyValue(def2);

  if (pk1 == null || pk2 == null) return false;

  // Compare table names
  if (def1.tableName != def2.tableName) return false;

  // Compare connections if both are OrmConnections
  if (connection is OrmConnection && other.connection is OrmConnection) {
    // Type promotion works when checking directly on the property
    final thisName = (connection as OrmConnection).name;
    final otherName = (other.connection as OrmConnection).name;
    if (thisName != otherName) {
      return false;
    }
  }

  return pk1 == pk2;
}