I'm having problems using weight enabled node with dynamic query. From my module code:

// Fetch the data from the database, ordered by weight ascending.
  $select = db_select('node', 'n');
  // Join tables
  $select->join('weight_settings', 'ws', 'n.type = ws.type');
  $select->join('weight_weights', 'w', 'n.nid = w.entity_id');
  $select->fields('n', array('title', 'nid', 'type'));
  $select->fields('w', array('weight'));
    ->condition('n.status', '1')
    ->orderBy('weight', 'ASC');
  $select->range(0, 50);

If I create a new node with the standard node edit form and accept a default weight = 0, then this new node will not be included in the results of this query. Looking in the database I discovered that at submit a record for the node was not being saved to {weight_weights}.

My work-around was to adjust the content type's default weight to equal '-1'. This saved a value to {weight_weights} and consequently returned the new node as part of the result.

I'm filing a bug, but realize that I also may not be constructing the query correctly. If the latter, recommendations are most welcome.

Also, I noticed that the revised default weight value was not being captured by Features. See http://drupal.org/node/1589948


#18 weight-setweight0-1589932-17.patch357 byteskenficara


davisben’s picture

Status:Active» Fixed

This has been fixed and committed to 7.x-2.x.

mbolli’s picture

Status:Fixed» Needs work

Since the fix I'm getting a PDOException upon saving nodes, because the third field in the weight_weights table is empty but not null. I have temporarily and dirty fixed it like this. It works, but the problem maybe lies elsewhere...

@@ -421,6 +421,9 @@
  * Set the weight of a node.
function _weight_set_weight($node) {
+  if(empty($node->weight_weight)) {
+    $node->weight_weight = 0;
+  }
     ->key(array('entity_id' => $node->nid))
davisben’s picture

Status:Needs work» Active

I'm unable to reproduce the error. Could you provide a little more info about your configuration? One thing to check is that the default weight is being applied to nodes when creating them.

mbolli’s picture

Thanks for your response. Actually it was a node of a content type where I haven't activated weight at all.
What would you like to know about my configuration? It's a Drupal 7.14 installation on Linux with Apache 2, PHP 5 and MySQL 5.

davisben’s picture

Status:Active» Fixed

That's all I needed, thanks! This has been fixed and committed.

rooby’s picture

Marked #1602692: Undefined property weight_weight in weight_node_insert() (line 342 of weight.module) as duplicate of this issue.

And adding this error message, which is fixed by this commit for the sake of people searching on error messages:

Notice: Undefined property: stdClass::$weight_weight in weight_node_insert() (line 342 of /var/www/mysite/sites/all/modules/contrib/weight/weight.module).

rooby’s picture

[removed duplicate post]

bvanmeurs’s picture

Status:Fixed» Active

This is not fixed at all. I downloaded the latest version and still get the errors:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'weight' at row 1

#0 /home/adrupal/www/includes/database/database.inc(2136): PDOStatement->execute(Array)
#1 /home/adrupal/www/includes/database/database.inc(664): DatabaseStatementBase->execute(Array, Array)
#2 /home/adrupal/www/includes/database/mysql/query.inc(36): DatabaseConnection->query('INSERT INTO {we...', Array, Array)
#3 /home/adrupal/www/includes/database/query.inc(1614): InsertQuery_mysql->execute()
#4 /home/adrupal/www/sites/all/modules/weight/weight.module(434): MergeQuery->execute()
#5 /home/adrupal/www/sites/all/modules/weight/weight.module(352): _weight_set_weight(Object(stdClass))
#6 [internal function]: weight_node_update(Object(stdClass))
#7 /home/adrupal/www/includes/module.inc(823): call_user_func_array('weight_node_upd...', Array)
#8 /home/adrupal/www/modules/node/node.module(1134): module_invoke_all('node_update', Object(stdClass))
#9 /home/adrupal/www/modules/node/node.pages.inc(406): node_save(Object(stdClass))
#10 /home/adrupal/www/includes/form.inc(1443): node_form_submit(Array, Array)
#11 /home/adrupal/www/includes/form.inc(854): form_execute_handlers('submit', Array, Array)
#12 /home/adrupal/www/includes/form.inc(374): drupal_process_form('webform_node_fo...', Array, Array)
#13 /home/adrupal/www/includes/form.inc(131): drupal_build_form('webform_node_fo...', Array)
#14 /home/adrupal/www/modules/node/node.pages.inc(15): drupal_get_form('webform_node_fo...', Object(stdClass))
#15 [internal function]: node_page_edit(Object(stdClass))
#16 /home/adrupal/www/includes/menu.inc(516): call_user_func_array('node_page_edit', Array)
#17 /home/adrupal/www/index.php(21): menu_execute_active_handler()
#18 {main}

I regularly import nodes and am getting errors all over the place due to the weight module. It's simply unacceptable.

bvanmeurs’s picture

Status:Active» Fixed

P.S. it seems to be fixed in the dev version. Using that one now. I hope that it will be merged quickly to the release version!


TechNikh’s picture

dev version fixed my issue

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bvanmeurs’s picture

Version:7.x-2.0-rc2» 7.x-2.1

I updated to weight 2.1 but encountered this issue again. It only appears when you save a node programmatically which doesn't have a weight explicitly assigned to it, or has an empty value assigned to it. I thought this was fixed in 2.1, but I reviews the code and saw no changes in the _weight_set_weight function. Imho, in this function, there should be a check; either refusing to save the value to correct it to the default when it is incorrect. Right now, I get the following error on saving a general page node (which does not has weight enabled):

1 exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'weight' at row 1' in /home/ms2/www/includes/database/database.inc:2136
2 Stack trace:
3 #0 /home/ms2/www/includes/database/database.inc(2136): PDOStatement->execute(Array)
4 #1 /home/ms2/www/includes/database/database.inc(664): DatabaseStatementBase->execute(Array, Array)
5 #2 /home/ms2/www/includes/database/mysql/query.inc(36): DatabaseConnection->query('INSERT INTO {we...', Array, Array)
6 #3 /home/ms2/www/includes/database/query.inc(1614): InsertQuery_mysql->execute()
7 #4 /home/ms2/www/sites/all/modules/weight/weight.module(434): MergeQuery->execute()
8 #5 /home/ms2/www/sites/all/modules/weight/weight.module(352): _weight_set_weight(Object(stdClass))
9 #6 [internal function]: weight_node_update(Object(stdClass))
10 #7 /home/ms2/www/includes/module.inc(823): call_user_func_array('weight_node_upd...', Array)
11 #8 /home/ms2/www/modules/node/node.module(1134): module_invoke_all('node_update', Object(stdClass))
12 #9 /home/ms2/www/modules/node/node.pages.inc(406): node_save(Object(stdClass))
13 #10 /home/ms2/www/includes/form.inc(1443): node_form_submit(Array, Array)
14 #11 /home/ms2/www/includes/form.inc(854): form_execute_handlers('submit', Array, Array)
15 #12 /home/ms2/www/includes/form.inc(374): drupal_process_form('page_node_form', Array, Array)
16 #13 /home/ms2/www/sites/all/modules/compare/operations/compare_operation_submit_form.php(57): drupal_build_form('page_node_form', Array)
17 #14 /home/ms2/www/sites/all/modules/compare/operations/compare_operation_type_edit_node.php(84): compare_operation_submit_form->execute()
18 #15 /home/ms2/www/sites/all/modules/compare/operations/compare_operation_type.php(112): compare_operation_edit_node->execute()
19 #16 /home/ms2/www/sites/all/modules/compare/compare.module(531): compare_operation->get_result()
20 #17 /home/ms2/www/sites/all/modules/compare/drush/compare.drush.inc(107): _compare_get_results(NULL, Array, Array)
21 #18 [internal function]: drush_compare_export('/home/ms2/temp/...')
22 #19 /usr/share/drush/includes/command.inc(120): call_user_func_array('drush_compare_e...', Array)
23 #20 /usr/share/drush/includes/command.inc(58): drush_invoke_args('compare-export', Array)
24 #21 [internal function]: drush_invoke('compare-export', '/home/ms2/temp/...')
25 #22 /usr/share/drush/includes/command.inc(721): call_user_func_array('drush_invoke', Array)
26 #23 [internal function]: drush_command('/home/ms2/temp/...')
27 #24 /usr/share/drush/includes/command.inc(204): call_user_func_array('drush_command', Array)
28 #25 /usr/share/drush/drush.php(101): drush_dispatch(Array)
29 #26 /usr/share/drush/drush.php(41): drush_main()
30 #27 {main}

How about the following solution?

* Set the weight of a node.
function _weight_set_weight($node) {
  if (empty($node->weight_weight)) {
    $node->weight_weight = 0;
    ->key(array('entity_id' => $node->nid))
      'entity_id' => $node->nid,
      'entity_type' => 'node',
      'weight' => $node->weight_weight,
      'weight' => $node->weight_weight,

I hope you can fix this soon. I've been having this issue with the weight module some time now and have reverted to using 'old style' integer fields which is a shame as the idea behind your module is great.


bvanmeurs’s picture

Status:Closed (fixed)» Active

Changed the status to active, as this bug wasn't fixed.

davisben’s picture

Could you give an example of the code you're using that is causing the error?

davisben’s picture

Status:Active» Closed (cannot reproduce)

I'm unable to reproduce this.

davisben’s picture

Issue summary:View changes

updated with link to related issue

knalstaaf’s picture

Title:Weight not saving value if default = 0» Weight not saving default value
Version:7.x-2.1» 7.x-2.4
Issue summary:View changes
Status:Closed (cannot reproduce)» Active

I'm running into the same issue.

  • D7.34
  • Firefox 34.0 (latest) on Mac OSX 10.10.1 (Yosemite) (occurs in Chrome as well)
  • MySQL: server version 5.5.40-cll
  • PHP 5.3.27
  • jQuery Update 7.x-2.4 set to all possible values for administrative pages (default, 1.5, 1.7, 1.8, 1.10) - disabled this module and made another series of tests, but the issues persists
  • No error messages displayed
  • Module settings: default
  • Settings on content type: Enabled (Yes), Range (+50), Default weight (1, or any other number).

Cleared the weights, reset them, all to no avail.

If you manually set all the weights in the content overview to '1' for instance, it keeps this value for all the nodes. But having over 50 nodes, this is a tedious method. I tried to change the weight value in bulk, using Administration Views, but apparently AV doesn't recognize the weight field, eventhough it offers the field as a value to change. (I'm aware that AV doesn't play well with Weight (so far).)

I tried to move on to 7.x-3.x (since there's no 7.x-2.x-dev availabe), but I guess this is still in full development. I couldn't assign weight options to content types (did run the module updates).

Hopefully this information provides a clue to the issue. I'm not sure if this is of any importance, but there's a tilde in the url of the project we're working on (domain.com/~project). If more information is required, ask away.

kenficara’s picture

This is a related problem, or maybe the same problem -- I'm using 7.x-2.4 and have noticed that users are unable to set a weight of zero. Fixed this by changing the check in _weight_set_weight to check if $weight is set, rather than if it is truthy:

if ($node->nid && isset($weight)) {
kenficara’s picture

new357 bytes
jayhawkfan75’s picture

@kenficara: This was a pain. Patch #18 solved this issue for me. Thanks so much.

Christian DeLoach’s picture

Patch #18 solved this issue for me too. Thanks kenficara.

deggertsen’s picture

Status:Active» Reviewed & tested by the community

Thank you for #18. Very annoying problem, very simple fix. RTBC

davisben’s picture

Status:Reviewed & tested by the community» Closed (duplicate)

This was fixed with #2392401: Weight 0 not persistent.