DB Tables and Unique or duplicate values

You can define a logger in some code like:

logger = system.util.logger('what i want to show in the gateway logger column')

Then you can call that whenever with:

logger.info('message')

Just a way to setup the name of your logger at the top of your code without having to retype it for each message.

try:
    system.db.runNamedQuery('query', param)

except Exception as e:
    msg = str(e.getCause())
    if 'Cannot insert duplicate key' in msg:
        ### code to popup a window with a message, or whatever else you want to do with it ###

@tyrel.parker,

Ok, so I have defined the logger in the window open script like thisā€¦

logger = system.util.getLogger(ā€œbinderLoggerā€)

This way I can call if from any component in the window. (if I understand that correctly.)

I added this to my codeā€¦

try:
        #Update binder_page table
	system.db.runNamedQuery("page_name_update", {"new_page" :newPage, "topic_id" :topic_id})
			
except Exception as e:
	msg = str(e.getClause())
	logger.info('except javalang: {}'.format( msg)
	if 'duplicate key value violates unique constraint' in msg:
		logger.info( 'Page Name Already Exists.')
					
	else:
	pass
					

For some reason it does not like the if statement. says there is no alternative to the if. I must be missing something or syntax is not just right.

Any idea?

Thanks, Steven

So I found a missing bracket on the logger.info line.

it does not seem to affect anything. :frowning:

Still pops up the same error message.

Not sure where to go now.

Thanks, Steven

My guess is that you need to tab in the pass or leave off the else all together.

else:
    pass

I removed the else all together. I still get the same error message ā€¦

Traceback (most recent call last):
  File "<event:propertyChange>", line 24, in <module>
  File "<event:propertyChange>", line 24, in <module>
  Detail: Key (lower(page_name::text), topics_id)=(page 2, 9) already exists.

com.inductiveautomation.ignition.client.gateway_interface.GatewayException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "pagename_idx"
  Detail: Key (lower(page_name::text), topics_id)=(page 2, 9) already exists.

	caused by GatewayException: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "pagename_idx"
  Detail: Key (lower(page_name::text), topics_id)=(page 2, 9) already exists.
	caused by Exception: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "pagename_idx"
  Detail: Key (lower(page_name::text), topics_id)=(page 2, 9) already exists.

Ignition v8.0.16 (b2020082513)
Java: Azul Systems, Inc. 11.0.7

I guess it is not capturing the error.

Thanks, Steven

That error is from the gateway logs? Or when you click the button it gives you the error popup with that message?

@tyrel.parker it is in a popup after I press enter ( on property change).

Thanks, Steven

@steven.cox,

Did you figure this out?

Tyrel

You can follow this method I outline in another post after you make the UNIQUE indexes

I created a check prior to running the insert query so no error occurs. Made it nice and clean. I was checking for a blank anyway, so I just added to check for a duplicate as well.

Thanks for following up,

Steven