Drops.Relation.Schema.PrimaryKey (drops_relation v0.1.0)

View Source

Represents primary key information for a database table/schema.

This struct stores information about primary key fields, supporting both single-column and composite primary keys. Fields are stored as Field structs containing complete metadata.

Examples

# Single primary key
%Drops.Relation.Schema.PrimaryKey{
  fields: [%Drops.Relation.Schema.Field{name: :id, type: :integer, ...}]
}

# Composite primary key
%Drops.Relation.Schema.PrimaryKey{
  fields: [
    %Drops.Relation.Schema.Field{name: :user_id, type: :integer, ...},
    %Drops.Relation.Schema.Field{name: :role_id, type: :integer, ...}
  ]
}

# No primary key
%Drops.Relation.Schema.PrimaryKey{fields: []}

Summary

Functions

Gets the field names from the primary key.

Merges two primary keys, with the right primary key taking precedence.

Creates a new PrimaryKey struct.

Types

meta()

@type meta() :: %{composite: boolean()}

t()

@type t() :: %Drops.Relation.Schema.PrimaryKey{
  fields: [Drops.Relation.Schema.Field.t()],
  meta: meta()
}

Functions

field_names(primary_key)

@spec field_names(t()) :: [atom()]

Gets the field names from the primary key.

Examples

iex> field = Drops.Relation.Schema.Field.new(:id, :integer, :id, :id)
iex> pk = Drops.Relation.Schema.PrimaryKey.new([field])
iex> Drops.Relation.Schema.PrimaryKey.field_names(pk)
[:id]

merge(left, right)

@spec merge(t(), t()) :: t()

Merges two primary keys, with the right primary key taking precedence.

Parameters

  • left - The base primary key
  • right - The primary key to merge into the base, takes precedence

Returns

A merged Drops.Relation.Schema.PrimaryKey.t() struct.

Examples

iex> left = Drops.Relation.Schema.PrimaryKey.new([field1])
iex> right = Drops.Relation.Schema.PrimaryKey.new([field2])
iex> merged = Drops.Relation.Schema.PrimaryKey.merge(left, right)
iex> length(merged.fields)
1

new(fields)

@spec new([Drops.Relation.Schema.Field.t()]) :: t()

Creates a new PrimaryKey struct.

Parameters

  • fields - List of Field structs that form the primary key

Examples

iex> field = Drops.Relation.Schema.Field.new(:id, :integer, :id, :id)
iex> pk = Drops.Relation.Schema.PrimaryKey.new([field])
iex> length(pk.fields)
1
iex> pk.meta.composite
false