Stefan W Tue Jul 06 12:27:14 -0400 2010

Subject: Creating Tables Doesn't Work

I don't know if this is just PDO weirdness or an actual bug. Basically if I do any CREATE TABLE statement though find_by_sql it will fail with:

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error'

Annoyingly it doesn't elaborate on what the general error is and will in fact create the table - just die immediately afterwards.


$sql = 'CREATE TABLE temp_table (SELECT * FROM table)';

I suspect it may be down to the statement returning TRUE instead of with records but I don't know why it's throwing an exception.

For a bit of background I was using this to build temporary tables but as normal table creations fails in the same way it seemed like a more salient example.

Any pointers would be hugely helpful.


Kien La Tue Jul 06 18:15:36 -0400 2010

I'd suggest running query() against the connection object directly. You can either do:

YourModel::connection()->query('CREATE TABLE ...');

or grab this patch from the 1.1 branch which will let you do:

YourModel::query('CREATE TABLE ...');

Stefan W Wed Jul 07 11:44:04 -0400 2010

Thanks this works.

For anyone that happens to trying to use temporary tables the way I ended up doing it was to call

#create instance of the model
$model_object = new [model_name];

#run the temporary table creation query
$model_object->query($query_1, $query_1_data);

#... the connection to the database is still open so the temporary table is waiting to be used

#create the list of objects of the desired model using the temporary table's data
$record_set = $model_object->find_by_sql($query_2, $query_2_data);