With ORM, we can show the 3 tables required by the Web App in model:
Import time, Uuidfrom transwarp.db import next_idfrom transwarp.orm import Model, Stringfield, Booleanfield, Floatfield, T Extfieldclass User (Model): __table__ = ' users ' id = Stringfield (primary_key=true, default=next_id, ddl= ' varchar ()) E Mail = Stringfield (updatable=false, ddl= ' varchar ()) password = Stringfield (ddl= ' varchar ()) admin = Booleanfield () Name = Stringfield (ddl= ' varchar ()) image = Stringfield (ddl= ' varchar ()) Created_at = Floatfield (Updatable=false, Default=time.time) class Blog (Model): __table__ = ' blogs ' id = Stringfield (primary_key=true, default=next_id, ddl= ' Varch AR (+) ') user_id = Stringfield (updatable=false, ddl= ' varchar ()) user_name = Stringfield (ddl= ' varchar ()) User_ima GE = Stringfield (ddl= ' varchar ') name = Stringfield (ddl= ' varchar ()) Summary = Stringfield (ddl= ' varchar ()) CO Ntent = TextField () Created_at = Floatfield (Updatable=false, Default=time.time) class Comment (Model): __table__ = ' commen ts ' id = Stringfield (primAry_key=true, default=next_id, ddl= ' varchar ') blog_id = Stringfield (updatable=false, ddl= ' varchar ()) user_id = S Tringfield (updatable=false, ddl= ' varchar ') user_name = Stringfield (ddl= ' varchar ()) User_image = Stringfield ( ddl= ' varchar ') content = TextField () Created_at = Floatfield (Updatable=false, Default=time.time)
When writing an ORM, adding a default parameter to a field makes it easy for ORM to fill in the default values. Also, the default value can be passed in as a function object and calculated automatically when the insert () is called.
For example, the default value of the primary key ID is the function next_id, and the default value for the creation time Created_at is the function time.time, which automatically sets the current date and time.
Date and time are stored in the database in the float type, not the datetime type, so the benefit is not to worry about the time zone of the database and the time zone conversion problem, the sorting is very simple, when displayed, only need to do a float to STR conversion, also very easy.
Initializing database tables
If you have a small number of tables, you can create SQL scripts for your tables by hand:
--Schema.sqldrop database if exists awesome;create database awesome;use awesome;grant Select, INSERT, UPDATE, delete on a wesome.* to ' www-data ' @ ' localhost ' identified by ' www-data '; Create table users (' ID ' varchar () ' is not null, ' email ' varc Har () Not NULL, ' password ' varchar (+) NOT null, ' admin ' bool not NULL, ' name ' varchar (a) NOT null, ' image ' varchar (+) Not null, ' created_at ' real is not NULL, unique key ' idx_email ' (' email '), key ' Idx_created_at ' (' created_at '), PRI Mary Key (' ID ')) engine=innodb default Charset=utf8;create table blogs (' id ' varchar () not NULL, ' user_id ' varchar (50 ) not NULL, ' user_name ' varchar (a) NOT null, ' user_image ' varchar ($) NOT null, ' name ' varchar (#) NOT NULL, ' Summar Y ' varchar (mediumtext) not null, ' content ' is not null, ' created_at ' real is not null, key ' Idx_created_at ' (' Created_at ' ), primary key (' ID ')) engine=innodb default Charset=utf8;create table comments (' ID ' varchar () not NULL, ' blog_id ' varchar () not NULL, 'user_id ' varchar ' is not NULL, ' user_name ' varchar (IS) not null, ' user_image ' varchar ($) NOT NULL, ' content ' Mediumte XT NOT NULL, ' created_at ' real is not null, key ' Idx_created_at ' (' created_at '), primary key (' ID ')) engine=innodb default Charset=utf8;
If you have a large number of tables, you can automatically generate SQL scripts from the model object directly from the script, which is easier to use.
Put the SQL script on the MySQL command line to execute:
$ mysql-u Root-p < Schema.sql
We have completed the initialization of the database table.
Writing data access code
Next, you can actually start writing code operations objects. For example, for the user object, we can do the following:
# Test_db.pyfrom Models Import User, Blog, commentfrom transwarp import dbdb.create_engine (user= ' www-data ', password= ' Www-data ', database= ' awesome ') u = User (name= ' Test ', email= ' test@example.com ', password= ' 1234567890 ', image= ' about: Blank ') U.insert () print ' New User id: ', u.idu1 = User.find_first (' Where email=? ', ' test@example.com ') print ' Find User\ ' s n Ame: ', U1.nameu1.delete () U2 = User.find_first (' Where email=? ', ' test@example.com ') print ' Find User: ', U2
The MySQL client command line can be queried to see if the data is stored properly inside MySQL.