Why won't this Drupal 7 schema work? - database

I'm having issues with using the schema module and a table I created. I used schema to create the schema and then when I tried to install the module logo_management, I get this error:
Invalid default value for 'logoid':
CREATE TABLE {logo} ( `logoid` INT unsigned NULL auto_increment DEFAULT
0 COMMENT 'Unique Logo ID', `date_created` NULL DEFAULT 0 COMMENT
'The date the logo was added.', `category` VARCHAR(50) NULL DEFAULT
'OTHER' COMMENT 'The category the logo should be in.', `no_of_lines`
INT NULL DEFAULT 1 COMMENT 'Number of lines(1 or 2)',
`line_1` VARCHAR(100) NULL DEFAULT NULL COMMENT 'Line 1 for Logo',
`line_2` VARCHAR(100) NULL DEFAULT NULL COMMENT 'Line 2 for Logo',
`image_path` VARCHAR(100) NULL DEFAULT NULL COMMENT
'Path and filename for image', `activate`
INT NULL DEFAULT 0 COMMENT 'Boolean if the logo should be active or not.',
PRIMARY KEY (`logoid`) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT
Here is the .install schema I have. I'm assuming it's something wrong with the Invalid default value for 'logoid', however I tried putting the default value and it still gives me the same error.
function logo_management_schema() {
$schema['logo'] = array(
'description' => 'Logo Management Module',
'fields' => array(
'logoid' => array(
'description' => 'Unique Logo ID',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'date_created' => array(
'description' => 'The date the logo was added.',
'type' => 'date',
'not null' => TRUE,
),
'category' => array(
'description' => 'The category the logo should be in.',
'type' => 'varchar',
'length' => '50',
'not null' => TRUE,
'default' => 'OTHER',
),
'no_of_lines' => array(
'description' => 'Number of lines(1 or 2)',
'type' => 'int',
'not null' => TRUE,
'default' => 1,
),
'line_1' => array(
'description' => 'Line 1 for Logo',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'line_2' => array(
'description' => 'Line 2 for Logo',
'type' => 'varchar',
'length' => '100',
'not null' => FALSE,
),
'image_path' => array(
'description' => 'Path and filename for image',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'activate' => array(
'description' => 'Boolean if the logo should be active or not.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('logoid'),
);
return $schema;
}

Skill and determination fixed this problem! Changed the type to int.
$schema['logo'] = array(
'description' => 'Logo Management Module',
'fields' => array(
'logoid' => array(
'description' => 'Unique Logo ID',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
)
Then I ran into a date problem and changed it to this:
'date_created' => array(
'description' => 'The date the logo was added.',
'type' => 'datetime',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
Now my module installs!

Related

TYPO3 Custom Content fields on save database error

I added some content Fields which looks as it follows;
$temporaryColumnHC = array(
'my_type' => array(
'exclude' => 0,
'label' => 'My Type',
'config' => array(
'type' => 'select',
'renderType' => 'selectSingle',
'items' => array(
array('Bar', 'bar'),
array('Pie', 'pie'),
array('Donut', 'donut'),
array('Line', 'line'),
array('Bar 2', 'bar2'),
array('Bar 3', 'bar3'),
array('Bubble', 'bubble'),
)
)
),
'my_suffix' => array(
'exclude' => 0,
'label' => 'My Label Suffix',
'config' => array(
'type' => 'input',
'size' => 10,
'max' => 20
)
),
'my_source_url' => array(
'exclude' => 0,
'label' => 'Source URL',
'config' => array(
'type' => 'input',
'renderType' => 'inputLink'
)
),
'my_source' => array(
'exclude' => 0,
'label' => 'Source Text',
'config' => array(
'type' => 'text',
'cols' => 40,
'rows' => 15
)
),
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_content',
$temporaryColumnHC
);
but on save I get the following error
error [1620]: Unknown column 'suffix' in 'field list'
What I have to do that this fields is saved in database
As mentioned already as a comment, TYPO3 complains that the column is missing in the Database.
Your extension should provide an ext_tables.sql which extends the Database Schema. See https://docs.typo3.org/m/typo3/reference-coreapi/10.4/en-us/ExtensionArchitecture/FilesAndLocations/Index.html#ext-tables-sql for further information.
The file always contains CREATE TABLE statements, which will be converted by TYPO3 do update the schema.
Updating the schema can be done through the install tool of TYPO3, or tools like TYPO3 console via command line.

Drupal 7 Update Schema Breaking

Could someone tell me if I've missed something in this? It keeps throwing the error mentioned at the bottom after the code block.
function license_update_7101() {
$schema['license_agreements'] = array(
'description' => 'License User Agreements',
'fields' => array(
'license_id' => array(
'description' => 'Primary key for license records',
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE
),
'user_id' => array(
'description' => 'License agreed to by User ID',
'type' => 'int',
'not null' => FALSE,
'unsigned' => TRUE,
'default' => '0'
),
'product_id' => array(
'description' => 'Product ID that represents the product this agreement was assigned to.',
'type' => 'int',
'not null' => FALSE,
'unsigned' => TRUE,
'default' => '0'
),
'session' => array(
'description' => 'Session created during account creation.',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
'default' => ''
),
'agreed' => array(
'description' => 'Represents if the license was agreed to or not',
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
'unsigned' => TRUE,
'default' => '0'
),
'agreement_date' => array(
'description' => 'Date this license was agreed to by user.',
'type' => 'datetime',
'mysql_type' => 'DATETIME',
'disp-width' => '11',
'not null' => FALSE,
'default' => NULL
),
'blog_name' => array(
'description' => 'URL for actual product license belongs to.',
'type' => 'varchar',
'length' => '128',
'not null' => FALSE,
'default' => ''
),
'primary key' => array('license_id')
)
);
db_create_table('license_agreements', $schema['license_agreements']);
}
ERROR:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB [error]
server version for the right syntax to use near 'DEFAULT NULL
) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'License' at line 9
Really would love to get this solved.
Thanks!
So, courtesy of having another dev come help me try to find what was blowing things up, I managed to discover the issue as we worked through it.
'primary key' => array('license_id')
I ended up noticing that that was embedded within the fields array and needed to be outside/after it.
So, the fix was as simple as moving that line down as an array element of the license_agreements array. Problem solved!

cakephp testing controller: database mock doesn't return anything

I'm new to CakePHP and PHPUnit and I want to test one of my controllers. So far I have this method I like to test:
public function getAlternatives($id = null){
// ID parameter given or is NaN?
if (!$id || !is_numeric($id)) {
throw new BadRequestException();
}
// Error if object not found
if (!$this->Designitem->exists($id)) {
throw new NotFoundException();
}
$alternatives = $this->Designitem->getItemsWithFunritures($id);
$this->set(array(
'alternatives' => $alternatives,
'_serialize' => array('alternatives'),
'_jsonp' => true));
}
Now I want to test whether the getItemsWithFurnitures is called on my model Designitem. To test this I implemented this test:
public function testGetAlternativesWithValidID() {
$id = 1;
$url = array('controller' => 'Designitems', 'action' => 'getAlternatives', $id . '.json');
$expected = array(
'Designitem' =>
array(
'id' => 1,
'design_id' => 1,
'furniture_id' => 1,
'template_id' => 1,
),
);
$designitems = $this->generate('Designitems', array(
'methods' => array(
'getAlternatives',
),
'models' => array(
'Designitem' => array('getItemsWithFunritures'),
),
));
$designitems->Designitem
->expects($this->once())
->method('getItemsWithFunritures')
->will($this->returnValue($expected));
$result = $this->testAction($url);
$this->assertEquals(json_encode($expected), $result);
}
EDIT: My Designitems Fixture contains one stubbed entry. It looks like this:
class DesignitemFixture extends CakeTestFixture {
public $fields = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'design_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false),
'furniture_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false),
'template_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'templateitem_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'amount' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 5, 'unsigned' => false),
'feedback' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 20, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'clicks' => array('type' => 'integer', 'null' => true, 'default' => '0', 'unsigned' => false),
'ilike' => array('type' => 'boolean', 'null' => false, 'default' => null),
'dislike' => array('type' => 'boolean', 'null' => false, 'default' => null),
'dislike_reason' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'dislike_comment' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 2000, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'parent_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'has_alternatives' => array('type' => 'boolean', 'null' => true, 'default' => null),
'is_selected' => array('type' => 'boolean', 'null' => true, 'default' => null),
'is_recommendation' => array('type' => 'boolean', 'null' => true, 'default' => null),
'deleted' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 4, 'unsigned' => false),
'deleted_date' => array('type' => 'datetime', 'null' => true, 'default' => null),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);
public $records = array(
array(
'id' => 1,
'design_id' => 1,
'furniture_id' => 1,
'template_id' => 1,
'templateitem_id' => 1,
'amount' => 1,
'feedback' => 'Lorem ipsum dolor ',
'clicks' => 1,
'ilike' => 1,
'dislike' => 1,
'dislike_reason' => 'Lorem ipsum dolor sit amet',
'dislike_comment' => 'Lorem ipsum dolor sit amet',
'parent_id' => 1,
'has_alternatives' => 1,
'is_selected' => 1,
'is_recommendation' => 1,
'deleted' => 0,
'deleted_date' => '2016-07-14 12:05:39',
'created' => '2016-07-14 12:05:39',
'modified' => '2016-07-14 12:05:39'
),
);
}
When I run the test, I get an error that the getItemsWithFurnitures returned null:
Test result
Can anyone tell me what I'm doing wrong? Thank you!
I found a solution to the problem. My problem was that not the getItemsWithFurnitures returned null but the tested method getAlternatives isn't returning any value (-> null). So I have to check if the set value $this->set(...); is the same as the expected array. To achieve this I have to change the return method of the testAction(...) by passing array('return' => 'contents'). It will then return the produced content instead of the methods return value. The working test looks like this:
public function testGetAlternativesWithValidID() {
$id = 1;
$url = array('controller' => 'Designitems', 'action' => 'getAlternatives', $id . '.json');
$this->_generateMockWithAuthUserId('Designitems', 1);
$expected = array(
'Designitem' =>
array(
'id' => 1,
'design_id' => 1,
'furniture_id' => 1,
'template_id' => 1,
),
);
$data = $this->getMockForModel('Designitem', array('getItemsWithFunritures'));
$data->expects($this->once())
->method('getItemsWithFunritures')
->will($this->returnValue($expected));
$result = $this->testAction($url, array('return' => 'contents'));
// Delete all whitespaces from the result.
$result = preg_replace('/\s+/', '', $result);
// Add the surrounding alternatives array to the expected and convert to json-format.
$expected = json_encode(array('alternatives' => $expected));
$this->assertEquals($expected, $result);
}
Because I add these items into another array 'alternatives' while setting it to the view, I have to place the expected array in this 'alternatives'-array. I also had to replace all whitespaces because the json encoding methods used are different. In order to compare them they have to be exactly the same so I removed the whitespaces and the test passes.
I hope someone can use this info.

.install file not creating table in drupal

Hi i am very new in drupal. i just create a module that create a form with some inputs. I wrote .install file for the same but it is not creating table. While i am installing this module it is neither showing error nor install table.
function mycontact_schema() {
$schema['mycontact'] = array(
'description' => t('This table for mycontact.'),
'fields' => array(
'mycontctid' => array(
'description' => t('The primary identifier for a mycontact.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'vid' => array(
'description' => t('The current {mycontact_revisions}.vid version identifier.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'name' => array(
'description' => t('The {mycontact_name} of this mycontact.'),
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => ''),
'email' => array(
'description' => t('The name of this contact, always treated a non-markup plain text.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''),
),
'comments' => array(
'description' => t('The comments of this contact, always treated a non-markup plain text.'),
'type' => 'text',
'not null' => TRUE,
'default' => ''),
'primary key' => array('mycontctid'),
);
return $schema;
}
It is not showing any error and any warning.
There is nothing wrong in the hook_install implementation.
I followed these steps to get it working:
Created a directory called mycontact inside sites/all/modules.
Created a the files mycontact.info, mycontact.module, and mycontact.install.
Inside mycontact.info I added the following lines:
name = my contact
core = 7.x
And in the mycontact.install file, I copied all the code from the question as it is.
Left the mycontact.module file empty. Note: Drupal needs this empty file.
Enabled the module. And it worked!
What you could do now is, disable the module -> uninstall it -> follow the above steps to install the module; and you should have your database table created for you.

Drupal 7 hook_schema not installing database table

Any help would be brilliant.
function request_gold_pack_schema() {
$schema['request_gold_pack_customer_details'] = array(
'description' => 'Table to store all customer details.',
'fields' => array(
'rid' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'auto increment' => TRUE
),
'title' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'default' => ''
),
'first_name' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'last_name' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'house_name_no' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'street' => array(
'type' => 'varchar',
'length' => 160,
'not null' => TRUE,
'default' => ''
),
'town' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'county' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'telephone' => array(
'type' => 'int',
'length' => 12,
'not null' => TRUE,
'default' => ''
),
'email' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''
),
'date_registered' => array(
'mysql_type' => 'DATETIME',
'not null' => TRUE
),
'primary' => array(
'rid'
)
)
);
return $schema;
}
which gives me the following errors
Notice: Undefined index: type in DatabaseSchema_mysql->processField() (line 205 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc).
Notice: Undefined index: :normal in DatabaseSchema_mysql->processField() (line 205 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc).
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Table to stor' at line 13: CREATE TABLE {request_gold_pack_customer_details} ( rid INT NOT NULL DEFAULT 0, title VARCHAR(10) NOT NULL DEFAULT '', first_name VARCHAR(50) NOT NULL DEFAULT '', last_name VARCHAR(50) NOT NULL DEFAULT '', house_name_no VARCHAR(50) NOT NULL DEFAULT '', street VARCHAR(160) NOT NULL DEFAULT '', town VARCHAR(50) NOT NULL DEFAULT '', county VARCHAR(50) NOT NULL DEFAULT '', telephone INT NOT NULL DEFAULT '', email VARCHAR(255) NOT NULL DEFAULT '', date_registered DATETIME NOT NULL, primary DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Table to store all customer details.'; Array ( ) in db_create_table() (line 2688 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/database.inc).
Been trying to find a solution for hours.
Thanks.
It is an issue with your primary key. You hve it listed in the fields array (it should not be) and it should be referenced as 'primary key' and not 'primary', like so:
function request_gold_pack_schema(){
$schema['request_gold_pack_customer_details'] = array(
'description' => 'Table to store all customer details.',
'fields' => array(
// Your field definitions
),
'primary key' => array(
'rid'
)
);
return $schema;
}
Check out the schema api documentation on drupal.org.
I would also recommend setting the rid field to type serial and leaving off the auto increment parameter (Drupal will handle that). So the 'rid' field definitions would be as such:
'rid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
)
You have to specify the size in drupal 7 for type 'int'...i.e
'rid' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'auto increment' => TRUE,
),
the size can be normal,tiny,big
check drupal 7 datatypes.

Resources