1) I create a node type A (parent) and add a mandatory nodereference field to a node type B (child).
2) I add an A-node and several B-nodes referencing it:

A1
|-B1
|-B2
|-B3
|-...
Note that the nodereference is mandatory

3) There is nothing stopping me from deleting the parent A1 node at this moment. If I do that the child B-nodes are lost in limbo and the site becomes inconsistent.

When I say "I", I mean a regular site user, who is submitting content, i.e. someone who does not know (and doesn't care) what CCK is. Therefore this has to be bulletproof. When a mandatory relationship is established using nodereference, CCK should take care of it to the end: e.g. delete all child nodes when a parent is deleted (with a proper warning message) or simply make deletion impossible if referencing nodes exist.

Comments

yched’s picture

Category: bug » feature
Priority: Critical » Normal
Status: Active » Closed (won't fix)

Issues like referential integrity is far beyond nodereference's current scope.
Nodereference has no way to prevent a node from being deleted. see #147723: Deletion API for core for a massive core attempt that finally got abandonned.

As a side note, setting a field to 'required' doesn't mean you are guaranteed to find a value for all nodes. If you create nodes and add your 'required' field later on, then all your existing nodes still don't have any value for the field.
'Required' merely means you cannot submit the form without a value.

Also, 'critical bug' means the module is badly broken and cannot currently work.

kirilius’s picture

Thanks for the explanation.

I thought there must be a way in Drupal to enforce some kind of referential integrity. None of the existing (as much as they exist) relationship-oriented modules support that and nodereference looks like the simplest way of having a relationship between the nodes.

kirilius’s picture

I might try to create such functionality myself and delete the orphans by implementing the hook_nodeapi function. However I don't know the internals of Drupal and CCK. Can you please point me to a code snippet or some example of code that selects all referencing nodes for a given nid? A select statement would do.

yched’s picture

You'll need something along those lines (untested) :

$field = content_fields($field_name); // $field_name is the name of the nodereference field.
$db_info = content_database_info($field);
$query = 'SELECT nid FROM {'. $db_info['table'] . '} WHERE '. $field_name .'_nid = %d';
$result = db_query($query, $node->nid);

kirilius’s picture

Thanks a lot, I will try it.