One final adjustment is needed. If no column is identified as being the primary key, we don’t want to generate any part of the alter table… command. To accomplish this we enclose the entire block of text in an if condition which tests if any column has a primary_key attribute.
Note the new syntax which concatenates columns and primary_key with a dot. This says to look in the elements of the columns list for an item containing a primary_key key. The value the dotted key represents is the value of the first such key it encounters. The condition fails if there is none.
In the same manner, references may be made to elements inside nested dictionaries. Additional keys may be appended to reach any depth of nesting desired.