Class: Cybertrain::Schema::TableDef
- Inherits:
-
Object
- Object
- Cybertrain::Schema::TableDef
- Defined in:
- cybertrain/schema/table.rb
Overview
The t a create_table block yields.
Every table also gets id INTEGER PRIMARY KEY AUTOINCREMENT; do not
declare it. The types are those below; there is no decimal, bigint,
json, binary or time. default: is SQL literal text, and a new
record starts with it when it is a plain literal.
Instance Method Summary collapse
-
#boolean(name, null: true, default: nil, limit: 0) ⇒ nil
A
BOOLEANcolumn (0/1), true/false in the model. -
#date(name, null: true, default: nil, limit: 0) ⇒ nil
A
DATEcolumn, a String ("2026-10-05") in the model. -
#datetime(name, null: true, default: nil, limit: 0) ⇒ nil
A
DATETIMEcolumn, a UTC Time in the model (stored as"2026-10-05T09:00:00Z", to the second). -
#float(name, null: true, default: nil, limit: 0) ⇒ nil
A
REALcolumn, a Float in the model. -
#integer(name, null: true, default: nil, limit: 0) ⇒ nil
An
INTEGERcolumn, an Integer in the model. -
#references(name, null: false, foreign_key: true) ⇒ nil
Adds “
_id" (Integer), an index on it, and -- unless `foreign_key: false` -- a foreign key to the pluralized table name ("post" -> "posts", "person" -> "people" via Cybertrain::Inflector). -
#string(name, null: true, default: nil, limit: 0) ⇒ nil
A
VARCHARcolumn, a String in the model. -
#text(name, null: true, default: nil, limit: 0) ⇒ nil
A
TEXTcolumn, a String in the model. -
#timestamps ⇒ nil
created_atandupdated_at, NOT NULL datetimes that Model#save fills in.
Instance Method Details
#boolean(name, null: true, default: nil, limit: 0) ⇒ nil
A BOOLEAN column (0/1), true/false in the model.
181 182 183 |
# File 'cybertrain/schema/table.rb', line 181 def boolean(name, null: true, default: nil, limit: 0) push_column(name, :boolean, null, default, limit) end |
#date(name, null: true, default: nil, limit: 0) ⇒ nil
A DATE column, a String ("2026-10-05") in the model.
206 207 208 |
# File 'cybertrain/schema/table.rb', line 206 def date(name, null: true, default: nil, limit: 0) push_column(name, :date, null, default, limit) end |
#datetime(name, null: true, default: nil, limit: 0) ⇒ nil
A DATETIME column, a UTC Time in the model (stored as
"2026-10-05T09:00:00Z", to the second).
194 195 196 |
# File 'cybertrain/schema/table.rb', line 194 def datetime(name, null: true, default: nil, limit: 0) push_column(name, :datetime, null, default, limit) end |
#float(name, null: true, default: nil, limit: 0) ⇒ nil
A REAL column, a Float in the model.
169 170 171 |
# File 'cybertrain/schema/table.rb', line 169 def float(name, null: true, default: nil, limit: 0) push_column(name, :float, null, default, limit) end |
#integer(name, null: true, default: nil, limit: 0) ⇒ nil
An INTEGER column, an Integer in the model.
157 158 159 |
# File 'cybertrain/schema/table.rb', line 157 def integer(name, null: true, default: nil, limit: 0) push_column(name, :integer, null, default, limit) end |
#references(name, null: false, foreign_key: true) ⇒ nil
Adds “
The foreign key is what gives the two models their association
readers (comment.article, article.comments).
223 224 225 226 227 228 229 230 231 |
# File 'cybertrain/schema/table.rb', line 223 def references(name, null: false, foreign_key: true) column_name = "#{name}_id" push_column(column_name, :integer, null, nil, 0) @table.add_index(Index.new(@table.name, [column_name], false)) if foreign_key to_table = Cybertrain::Inflector.pluralize(name.to_s) @table.add_foreign_key(ForeignKey.new(@table.name, column_name, to_table)) end end |
#string(name, null: true, default: nil, limit: 0) ⇒ nil
A VARCHAR column, a String in the model.
133 134 135 |
# File 'cybertrain/schema/table.rb', line 133 def string(name, null: true, default: nil, limit: 0) push_column(name, :string, null, default, limit) end |
#text(name, null: true, default: nil, limit: 0) ⇒ nil
A TEXT column, a String in the model.
145 146 147 |
# File 'cybertrain/schema/table.rb', line 145 def text(name, null: true, default: nil, limit: 0) push_column(name, :text, null, default, limit) end |
#timestamps ⇒ nil
created_at and updated_at, NOT NULL datetimes that
Model#save fills in.
237 238 239 240 |
# File 'cybertrain/schema/table.rb', line 237 def push_column("created_at", :datetime, false, nil, 0) push_column("updated_at", :datetime, false, nil, 0) end |