-
Notifications
You must be signed in to change notification settings - Fork 7
en database table
To use jActiveRecord, the table has to contain below columns:
- An auto increment primary key
id. - Two
timestamptype columns, namedcreated_atandupdated_at.
It's recommended to use jActiveRecord following below advices:
- Databale Table - Plural with underscores separating words. i.e., users, blog_tags, etc.
- Association
hasOneandbelongTo- Singular with underscores separating words. i.e., users, blog_tags, etc. - Association
hasManyandhasAndBelongToMany- Plural with underscores separating words. i.e., users, blog_tags, etc. - Variable of
Tabletype - Singular with the first letter of each word capitalized. i.e. User, BlogTag, etc. - Variable of
Recordtype - Singular with Camel Case. i.e. user, blogTag, etc. - Variable of
List<Record>type - Plural with Camel Case. i.e. users, blogTags, etc. - Foreign key - Singular of table name with "_id" suffix. i.e.
user_id,blog_tag_id, etc.
Regarding above, it's recommended to use DB#createTable(String name, String... columns) to create table. The table name is like [catalog.][schema.]table, while catalog and schema are optional, same with below. In addition, createTable would create the auto increment primary key id, created_at and updated_at as well.
DB dbo = DB.open("URL");
Table User = dbo.createTable("users", "name text", "age integer");DB#dropTable(String name) used for drop an existing table.
DB dbo = DB.open("URL");
dbo.dropTable("users");DB#active(String name) used for fetch a Table instance by specified name. The same table can be activied more than one times.
DB dbo = DB.open("URL");
Table User = dbo.active("users");DB#createIndex(String name, String table, String... columns) used for create a named index in table:
DB dbo = DB.open("URL");
dbo.createIndex("user_index", "users", "id");DB#dropIndex(String name, String table) used for drop named index:
DB dbo = DB.open("URL");
dbo.dropIndex("user_index", "users");- 博客:zzp.me
- 微博:@redraiment