Skip to content

Migrator

createTable()

createTable() — returns TableDefinition

Available in: migration Category: Migration Functions

The createTable() function is used in migration CFCs to define a new database table. It returns a TableDefinition object, on which you can specify columns, primary keys, timestamps, and other table properties. Once the table is defined, you call create() to actually create it in the database.

NameTypeRequiredDefaultDescription
namestringyesThe name of the table to create
forcebooleannofalsewhether to drop the table before creating it
idbooleannotrueWhether to create a default primarykey or not
primaryKeystringnoidName of the primary key field to create
// Example: create a users table
t = createTable(name='users'); 
	t.string(columnNames='firstname,lastname', default='', allowNull=false, limit=50);
	t.string(columnNames='email', default='', allowNull=false, limit=255); 
	t.string(columnNames='passwordHash', default='', allowNull=true, limit=500);
	t.string(columnNames='passwordResetToken,verificationToken', default='', allowNull=true, limit=500);
	t.boolean(columnNames='passwordChangeRequired,verified', default=false); 
	t.datetime(columnNames='passwordResetTokenAt,passwordResetAt,loggedinAt', default='', allowNull=true); 
	t.integer(columnNames='roleid', default=0, allowNull=false, limit=3);
	t.timestamps();
t.create();

// Example: Create a table with a different Primary Key
t = createTable(name='tokens', id=false);
	t.primaryKey(name='id', allowNull=false, type="string", limit=35 );
	t.datetime(columnNames="expiresAt", allowNull=false);
	t.integer(columnNames='requests', default=0, allowNull=false);
	t.timestamps();
t.create();

// Example: Create a Join Table with composite primary keys
t = createTable(name='userkintins', id=false); 
	t.primaryKey(name="userid", allowNull=false, limit=11);
	t.primaryKey(name='profileid', type="string", limit=11 );  
t.create();