Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Error trapping for referential-integrity triggers
Message
From
02/03/2006 15:21:07
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
02/03/2006 14:24:24
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 6
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01101036
Message ID:
01101050
Views:
10
As Nadya says, you must more or less code for this yourself, and try to intercept the message before VFP does. Consider referential integrity mainly a safeguard, which you wouldn't want to miss.

I created the following function, which I inserted into the framework, before trying to delete the record; instead of "trigger failed", the user will receive a message "Can't delete, there are related records in table XYZ". For a few specific cases, I recently created more specific functions, that tell give the user more detailed information.

I hope it doesn't include framework-specific details...
FUNCTION CanDelete()
	local lcHasChildRecords
	lcHasChildRecords = HasChildRecords()
	if not empty(lcHasChildRecords)
		ErrorMsg("Can't deleted; related data in table " + lcHasChildRecords + ".")
		return .F.
	endif
ENDFUNC



FUNCTION HasChildRecords()

	if CursorGetProp("SourceType") # 3	&& not a table
		return ""
	endif

	*!*	lParameters tlShowErrors
	*!*	if parameters() < 1
	*!*		tlShowErrors = .T.
	*!*	endif

	local lnSelect
	lnSelect = select()

	if not used("RelationsForDelete")
		open database database
		adbobjects(arelations, "relation")
		create cursor RelationsForDelete (child C(30), parent C(30), childtag C(10), parenttag C(10), ri C(10))
		append from array arelations
		select *;
			from RelationsForDelete;
			into cursor RelationsForDelete;
			where substr(ri, 2, 1) = "R"
		index on Parent tag Parent
	endif
	local lcError, laFields(1), lcTableName, lcChildTable, lcChildAlias, lcParentTag, lcChildTag,;
		lcSeekExpr, lcParentKey
	lcError = ""
	select (lnSelect)
	afields(laFields)
	lcTableName = upper(laFields(12))
	if empty(lcTableName)	&& probably a free table
		return ""
	endif
	use (dbf()) again alias "CanDelete_ParentTable" in 0
	select RelationsForDelete
	scan for parent = lcTableName
		lcParentTag = alltrim(ParentTag)
		lcChildTag = alltrim(ChildTag)
		lcChildTable = alltrim(Child)
		lcChildAlias = "CanDelete_" + lcChildTable
		select CanDelete_ParentTable
		set order to (lcParentTag)
		lcParentKey = key()
		select (lnSelect)
		lcSeekExpr = eval(lcParentKey)
		if used(lcChildAlias)
			select (lcChildAlias)
		else
			select 0
			use (lcChildTable) again alias (lcChildAlias)
		endif
		set order to (lcChildTag)
		if seek(lcSeekExpr)
			lcError = lcChildTable
			exit
		endif
	endscan

	select (lnSelect)
	use in CanDelete_ParentTable
	return lcError
ENDFUNC
>Hi all,
>
>In bringing our old FPD 2.5 system into VFP 6, I decided to take advantage of the Referential Integrity features. I understand the concept, but am having some trouble putting it into actual use.
>
>For example, I've got the following two tables:
>
>customer (parent)
>orders (child)
>
>One-to-many relationship (obviously) as one customer can have many orders.
>
>I set it up so that if someone tries to delete (for example) customer #200, that will be prevented if there are any orders out there for customer #200 (rules for deleting = restrict).
>
>Now, when I go into my system and try to delete customer #200, I'd like the system to tell the user something like, "Customer #200 cannot be deleted as they have open orders." Instead, I just get a message that says "Trigger failed," and the thing crashes.
>
>I'm assuming there's gotta be somewhere to put code to handle a failed trigger, but where?
>
>Thanks very much!
>
>John
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Reply
Map
View

Click here to load this message in the networking platform