Managing database and tables creation¶
You will need to create your database and tables before to use the module. Rethink:Model propose rethinkmodel.manage
module wich includes some tools to make this.
Note
Because the manage
module will import your package, we strongly recommend to declare your models in a separated module where there is no code to execute.
Python way¶
It’s possible to use create database and tables when you start your project (e.g. with Flask). In your main module (the script you launch to start your project), import the module where you defined your models. Then, use the connect()
, checkdb()
and auto
functions:
from rethinkmodel
from rethinkmodel.manage as manage
import mydata # this is your package or module
rethinkmodel.config(dbname="mydatabase")
manage.check_db() # creates the database if needed
manage.manage(mydata) # will introspect your module
Above code will find the entire list of models that you defined and create tables. If you defined get_indexes()
methods, the indices are created in RethinkDB.
It’s also possible to use package name instead of importing it.
from rethinkmodel
from rethinkmodel.manage as manage
rethinkmodel.config(dbname="mydatabase")
manage.check_db() # creates the database if needed
# here, the module is imported by the manage method
manage.manage("mydata")
Shell way¶
Another possibility is to call the rethinkmodel.manage
from command line giving the path to your data modules.
In this case, you’ll need to use evironment variable to define where is your database, user name, passowrd, port… see rethinkmodel.db
documentation to know the list of variables
export RM_DBNAME="mydatabase"
python -m rethinkmodel.manage path/to/data