Clear ORM
  • Welcome to Clear
  • Introduction
    • Setup
  • Model
    • Defining your model
      • Describing your columns
      • Primary Keys
      • Converters
    • Associations
      • belongs_to
      • has_many
      • has_many through
      • has_one
    • Lifecycle
      • Persistence
      • Validations
      • Triggers
    • Batchs operations
      • Bulk update
      • Bulk insert & delete
    • Transactions & Save Points
      • Transaction & Savepoints
      • Connection pool
    • Locks
  • Querying
    • The collection object
      • Filter the query
        • Filter the query – The Expression Engine
        • Find, First, Last, Offset, Limit
        • Aggregation
        • Ordering & Group by
      • Fetching the query
        • Each and Fetch
        • Cursored fetching
        • Model extra attributes
      • Joins
      • Eager Loading
      • Window and CTE
      • Scopes
    • Writing low-level SQL
      • Select Clause
      • Insert Clause
      • Delete Clause
  • Migrations
    • Manage migrations
    • Call migration script
    • Migration CLI
  • Additional and advanced features
    • JSONB
    • Symbol vs String
    • Enums
    • BCrypt
    • Full Text Search
    • Handling multi-connection
  • Other resources
    • API Documentation
    • Inline documentation
    • Github repository
    • Credits
    • Benchmark
Powered by GitBook
On this page
  1. Model
  2. Defining your model

Primary Keys

Clear needs your model to define a primary key column. By default, Clear can handle properly int, bigint, string and uuid primary keys.

As time of writing this manual, compound primary keys are not handled properly.

primary_key helper

Clear offers a built-in primary_key helper which will define your primary key without hassle:

class Product
  include Clear::Model

  self.table = "products"

  primary_key name: "product_id", type: :uuid
end
  • name is the name of your column in your table. (Default: id)

  • type is the type of the column in your table. Set to (Default: bigserial).

  • By default, types can be of type bigserial, serial, int, bigint, text and uuid.

Note than primary_key directive in the model class is just a fast way of writing:

column id : Int64, primary: true, presence: false

The primary key name is ID, of type bigint and it won't check the presence on save because it has default value

In case of uuid, Clear will generate a new uuid at every new object creation before inserting it into the database.

PreviousDescribing your columnsNextConverters

Last updated 5 years ago