PDOException: Column not found - drupal-7

I have just taken over a Drupal 7.14 website that an old colleague made. I have logged in, but whenever I go to Admin > Content and click 'edit' to edit the content of any of my basic pages I get the following error message:
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'source' in 'where clause': SELECT url_alias.* FROM {url_alias} url_alias WHERE (source = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] => node/1 ) in path_load() (line 419 of /home/sites/mediamatterstechnology.com/public_html/includes/path.inc).
Also if I go into Admin > Configuration > URL Aliases I get a similar message which is shown below:
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'language' in 'where clause': SELECT 1 FROM {url_alias} WHERE language <> :language LIMIT 0, 1; Array ( [:language] => und ) in path_admin_overview() (line 18 of /home/sites/mediamatterstechnology.com/public_html/modules/modules/path/path.admin.inc).
I would be so grateful for any help with this. I have been working on it for days, but I am a newbie with Drupal.

Drupal 5 and Drupal 6 don't have one (or two) of those fields. The reason why that database doesn't contain those fields could be:
The site was update from Drupal 5 / Drupal 6 to Drupal 7, and the update was not successful
The database got corrupted
A module removed the fields from the database
Those fields were manually removed
What you can do is adding the missing fields to the database, trying to first update the existing fields (in the case the site was updated from a previous Drupal version, and the update was not successful).
The following code should help.
// Drop indexes.
#db_drop_index('url_alias', 'src_language_pid');
#db_drop_unique_key('url_alias', 'dst_language_pid');
// Rename the fields, and increase their length to 255 characters.
#db_change_field('url_alias', 'src', 'source', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
#db_change_field('url_alias', 'dst', 'alias', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
if (db_field_exists('url_alias', 'language')) {
$spec = array(
'description' => "The language this alias is for; if 'und', the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.",
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
);
db_change_field('url_alias', 'language', 'language', $spec);
db_update('url_alias')
->fields(array('language' => LANGUAGE_NONE))
->condition('language', '')
->execute();
}
else {
$spec = array(
'description' => "The language this alias is for; if 'und', the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.",
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
);
db_add_field('url_alias', 'language', $spec);
}
// Add indexes back.
#db_add_index('url_alias', 'source_language_pid', array('source', 'language', 'pid'));
#db_add_index('url_alias', 'alias_language_pid', array('alias', 'language', 'pid'));
If this code doesn't get the missing fields, then it was not a failed update. In this case, you can use the following code, which should be used when you are sure the site was not updated from a Drupal version that is earlier than Drupal 7.
// Drop indexes.
#db_drop_index('url_alias', 'src_language_pid');
#db_drop_unique_key('url_alias', 'dst_language_pid');
$spec = array(
'description' => "The language this alias is for; if 'und', the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.",
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
);
db_add_field('url_alias', 'language', $spec);
$spec = array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''
);
db_add_field('url_alias', 'source', $spec);
// Add indexes back.
#db_add_index('url_alias', 'source_language_pid', array('source', 'language', 'pid'));
#db_add_index('url_alias', 'alias_language_pid', array('alias', 'language', 'pid'));
The code has been written basing on the update code used from the System module when updating a previous Drupal version. The part I added is the one to create the database fields, since database fields are normally created during a Drupal installation.

Related

CakePHP Migrations - How to specify scale and precision

I'm Running CakePhp 2.7 with Migrations Plugin and a Postgresql DB.
Creating a field of type 'number' and specifying length 15,4 (scale 15, precision 4 - or any length) does not actually create the field with that precision and/or scale.
...
'license_fee' => array(
'type' => 'number',
'null' => true,
'length' => '15,6',
'default' => 0
),
...
The field is created with the correct type (numeric) but with no scale/precision here is the Postgres description of the created field.
license_fee | numeric | default 0
What I was expecting to see is this
license_fee | numeric(15,6) | default 0
I also tried using 'type' => 'decimal' but same happened. This might not be supported by the migrations plugin but I just want to know if anyone knows for sure what's going on.
Found here: http://docs.phinx.org/en/latest/migrations.html
In order to create a : decimal(9,3)
$table->addColumn('distance', 'decimal', [
'default' => null,
'null' => false,
'precision'=>9,
'scale'=>3
]);
After further investigation and some help from Cake Development Corp. It turns out that the correct to way specify precision and scale is by using "limit" not "length" like I was attempting. So it should be like this:
'license_fee' => array(
'type' => 'number',
'null' => true,
'limit' => '15,6', //this is where I was wrong by using length
'default' => 0
),
This will work also if using 'type' => 'decimal' which is actually the same datatype. The end result is as expected:
license_fee | numeric(15,6) | default 0
I hope this useful for someone.
For 3.10 version:
$table->addColumn('name', ['type' => 'decimal', 'length' => 10, 'precision' => 3])
At /vendor/cakephp/cakephp/src/Database/Schema/TableSchema.php are the valid keys that can be used in a column:
$_columnKeys = [
'type' => null,
'baseType' => null,
'length' => null,
'precision' => null,
'null' => null,
'default' => null,
'comment' => null,
];

Drupal 7 failure to create an entity of a fieldable type I created because of null id

I am writing my first module and have created a fieldable entity. The only property of the entity in its schema is an 'id' which is type serial, unsigned and not null. However when I use entity_create (with no property values set) followed by $entity->save() the code fails with the following SQL error:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'entity_id' cannot be null: INSERT INTO {field_data_field_available} (entity_type, entity_id, revision_id, bundle, delta, language, field_available_value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => appointments_status [:db_insert_placeholder_1] => [:db_insert_placeholder_2] => [:db_insert_placeholder_3] => appointments_status [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => No ) in field_sql_storage_field_storage_write() (line 514 of C:\wamp\www\Drupal\modules\field\modules\field_sql_storage\field_sql_storage.module).
This implies to me that Drupal is trying to create entries in the 'field_data' table before it has created the entry in the entity table. I set no property values because I only have the 'id' property and surely that should be auto generated. If this was done first then it would have the id for creating the entry in the field_data table. The exact code I used is:
$entity_record = entity_create($entity_name,array());
$entity_record->save();
I hope someone has clues. The only work around I can see is to count the existing records, add one and use that as the new id but I can see loads of issues with this approach (clashing ids, performance etc).
Work around code:
$entities = entity_load('my entity type');
$entity_id = count($entities) + 1;
// Create the new entity object with this ID
$entity = entity_create('my entity type, array('id' => $entity_id));
// Save the new entity ready to reload after this if block of code
$entity->save();
My hook_entity_info is:
function appointments_entity_info() {
return array(
'appointments_status' => array(
'label' => t('Appointment status'),
'default label' => t('Appointment status'),
'plural label' => t('Appointment statii'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIController',
'views controller class' => 'EntityDefaultViewsController',
'base table' => 'appointments_status',
'entity keys' => array(
'id' => 'id'
),
'fieldable' => TRUE,
// Create one default bundle of the same name
'bundles' => array(
'appointments_status' => array(
'label' => t('Appointment statii'),
),
),
// Use the default label() and uri() functions
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'appointments',
),
);
}
Thanks
Rory
Thanks very much to #Arkreaktor for pointing me in the right direction. I added a 'bundle' property to my schema - type varchar length 255. I made no changes to hook_entity_info to refer to this property but then changed my entity create code to:
$entity = entity_create($entity_type, 'bundle' = $entity_type);
$entity->save();
$entity_id = $entity->id;
// Re-load the new entity or loading the existing entity
$entity = entity_load_single($entity_type, $entity_id);
// Rebuild the entity object from the values on the form
entity_form_submit_build_entity($entity_type, $entity, $form, $form_state);
// Save updated entity
$entity->save();
And it worked!!!
I hope this helps others.
Rory

drupal form field not loading correct data

I am building my first Drupal 7 module and am having trouble with the screen to edit a fieldable entity. I am using field_attach_form and it is working great for all accept one field which is displaying the field default rather than the current content of that field for that entity.
I have a text field, a number field, a number of Boolean fields and the one list_text field which is failing.
Any ideas what I a doing incorrectly? Code below is what I think is needed but please do let me know if you need more.
Code to create the field in hook_enable:
if (!field_info_field('field_available')) {
$field = array (
'field_name' => 'field_available',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array('No', 'Provisionally', 'Yes'),
),
);
field_create_field($field);
Code to create the instance, also in hook_enable:
if (!field_info_instance('appointments_status', 'field_available', 'appointments_status')) {
$instance = array(
'field_name' => 'field_available',
'entity_type' => 'appointments_status',
'bundle' => 'appointments_status',
'label' => t('Available?'),
'required' => TRUE,
'default_value' => array(array('value' => 'No')),
'description' => t('Set to No if appointments with this status make this slot unavailable, Provisionally means that it will only reserve a space temporarily'),
);
field_create_instance($instance);
This entity has only the one bundle with the same name as the entity.
The code to create the URL in hook_menu:
$items['admin/appointments/appointments_statii/%/edit'] = array(
'title' => 'Edit appointment status',
'description' => 'Edit the parameters of the selected status code',
'page callback' => 'drupal_get_form',
'page arguments' => array('appointments_status_edit_form',3),
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
);
The form function is:
function appointments_status_edit_form($form, &$form_state) {
// Get the status id from the form_state args
$status_id = $form_state['build_info']['args'][0];
// Load the chosen status entity
$status = entity_load_single('appointments_status', $status_id);
// Set up the fields for the form
field_attach_form('appointments_status', $status, $form, $form_state);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save changes',
'#weight' => 99,
);
return $form;
}
I have used the Devel module's dpm to check that the data is loaded correctly by entity_load_single and it is.
Thanks
Rory
I have answered my own question!
I was also programmatically loading some entities and was not loading this field with the numbers that a list_text field stores, instead I was loading the visual text.
I used a metadata wrapper and the code looked like this:
$w_appointments_status->$appointments_availability= 'Yes';
I changed it to:
$w_appointments_status->$appointments_availability = 2;
In this example 'Yes' was the third allowed value - hence 2.
So the code in my question was in fact correct although I have since added 'widget' and 'formatter' parameters to the instance.
I am sorry if this got some of you scratching your heads thinking ' but that code is correct'!!
Regards
Rory

Cakephp: find() Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Interestslogs.interest_id' in 'field list'

I have a table called 'Interestslogs' and model's name is Interestlog.
I need to get the client_id according to id from that table in Cakephp.
$client_id = $this->Interestslog->find('first',array(
'conditions' => array('Interestslogs.id' => $id),
'fields' => array('Interestslogs.client_id'),
)
);
However I am getting database error:
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Interestslogs.interest_id' in 'field list'
SQL Query: SELECT Interestslogs.interest_id FROM efa.interestslogs AS Interestslog LEFT JOIN efa.interests AS Interest ON (Interestslog.interest_id = Interest.id) LEFT JOIN efa.clients AS Client ON (Interestslog.client_id = Client.id) WHERE Interestslogs.id = 1 LIMIT 1
Remove the plural "s" form Interestslogs
$client_id = $this->Interestslog->find('first',array(
'conditions' => array('Interestslog.id' => $id),
'fields' => array('Interestslog.client_id'),
)
);
and also check your model. If every thing (Client and Interestslog) is associated properly you shouldn't get any error.
You can try: (if your relations are all right)
$this->Interestslog->recursive = -1;
$client_id = $this->Interestslog->find('first',array(
'conditions' => array('Interestslogs.id' => $id),
'fields' => array('Interestslogs.client_id'),
)
);
Check if there is interest_id column in your Interestlogs table.
Or try
$variable_temp = $this->Interestslog->findById($id);
//debug($variable_temp);
$client_id = $variable['client_id'];
$client_id = $this->Interestslog->find('first',array(
'conditions' => array('Interestslog.id' => $id),
'fields' => array('Interestslog.client_id'),
)
);
You should write the table names in the condition/field array without the last "s" since it's added by cakephp automatically.

Drupal 7 database API error "You have an error in your SQL syntax"

Using the following code:
db_update('nodesequence_nodes')
->fields(array(
'order' => 1,
))
->condition('nid', 1, '=')
->condition('nsid', 1, '=')
->execute();
I get the following error:
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 'order='1' WHERE (nid = '1') AND (nsid = '1')' at line 1: UPDATE
{nodesequence_nodes} SET order=:db_update_placeholder_0 WHERE (nid =
:db_condition_placeholder_0) AND (nsid = :db_condition_placeholder_1)
; Array ( [:db_update_placeholder_0] => 1
[:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => 1
) in nodesequence_init() (line 13 of
/var/www/eventbooking2/sites/all/modules/nodesequence/nodesequence.module).
I apologize I can't offer any more insight, but I hope you can.
The simple db_update code seems to me that it should work but I can't figure out why it isn't.
The database schema:
$schema['nodesequence_nodes'] = array(
'description' => 'Relating nodesequences to their consituent nodes.',
'fields' => array(
'nsid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
'order' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
),
'primary key' => array('nsid', 'nid'),
);
You can't use the column named 'order' in a table as this is a reserved word. You need to change it to something else.
More info here http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html take a look at table Table 9.2

Resources