Welcome Guest, Not a member yet? Register   Sign In
Issue modifying column using dbforge
#1

Hey guys, so im stuck on trying to edit a column in a table using the dbforge library. What seems to be happening, is its for some reason renaming the table first, before trying to change any of the other attributes... which is really weird to me.

Heres the error..
Quote:Unknown column 'FOO' in 'example_table'

ALTER TABLE `example_table` CHANGE `FOO` `BAR` varchar(15) NOT NULL, CHANGE `FOO` `FOO` varchar(20) NULL

The code snippet is below..

PHP Code:
<?php
$a
['original_name'] = 'FOO';
$a['column_name'  'BAR';

$fields[$a['original_name']] = array();

// Default values for all columns being edited
$fields[$a['original_name']]['name'       $a['column_name'];

// Dynamic values for edited columns
switch($a['type'])
{
 
   case 'text':
 
   case 'string':
 
       $fields[$a['original_name']]['type'       'varchar';
 
       $fields[$a['original_name']]['constraint' = (@is_numeric($a['max']) ? $a['max'] : '255');
 
       $fields[$a['original_name']]['null'       = ($a['required'] ? FALSE TRUE);

 
       if($a['default'])
 
           $fields[$a['original_name']]['default'] = $a['default'];
 
       break;

 
   case 'select':
 
       $fields[$a['original_name']]['type'       "enum('" implode("','"$a['options']) . "')";
 
       $fields[$a['original_name']]['null'       = ($a['required'] ? FALSE TRUE);
 
       $fields[$a['original_name']]['default'    $a['default'];
 
       break;

 
   case 'multiselect':
 
       $fields[$a['original_name']]['type'       "set('" implode("','"$a['options']) . "')";
 
       $fields[$a['original_name']]['null'       = ($a['required'] ? FALSE TRUE);

 
       if($a['default'])
 
           $fields[$a['original_name']]['default'] = implode(','$a['default']);
 
       break;

 
   case 'boolean':
 
       $fields[$a['original_name']]['type'       "enum('0','1')";
 
       $fields[$a['original_name']]['null'       FALSE;
 
       $fields[$a['original_name']]['default'    = ($a['default'] ? '1' '0');
 
       break;
}

// Change the table or log the error
self::$dbf->modify_column($partition_table$fields); 

Any ideas?

Thanks!
Reply
#2

And I just did a print_r of $fields, the output is exactly what I would expect..

Quote:Array
(
[FOO] => Array
(
[name] => BAR
[type] => varchar
[constraint] => 255
[null] => 1
[default] => Default Str Val
)

)
Reply
#3

Quote:ALTER TABLE `example_table` CHANGE `FOO` `BAR` varchar(15) NOT NULL, CHANGE `FOO` `FOO` varchar(20) NULL

The MySQL documentation is not very clear on when it is possible to change both the name and type of the column, so it is usually done as two separate operations. The error seems to indicate that you were trying to change "Foo varchar(15) NOT NULL" into "BAR varchar(20) NULL", and it would appear that dbforge butchered the reversed the order of the operations, or should have used the new name when changing the definition of the column.

However, that's not really clear from the code you supplied, and certainly doesn't match the print_r output you pasted.
Reply
#4

(07-02-2015, 01:49 PM)mwhitney Wrote:
Quote:ALTER TABLE `example_table` CHANGE `FOO` `BAR` varchar(15) NOT NULL, CHANGE `FOO` `FOO` varchar(20) NULL

The MySQL documentation is not very clear on when it is possible to change both the name and type of the column, so it is usually done as two separate operations. The error seems to indicate that you were trying to change "Foo varchar(15) NOT NULL" into "BAR varchar(20) NULL", and it would appear that dbforge butchered the reversed the order of the operations, or should have used the new name when changing the definition of the column.

However, that's not really clear from the code you supplied, and certainly doesn't match the print_r output you pasted.

Hmm, you may be right, ill give it a shot at modifying the type first, then renaming it.

Thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB