py-mysql2pgsql icon indicating copy to clipboard operation
py-mysql2pgsql copied to clipboard

Add handling of referential actions for foreign key constraints.

Open fvbock opened this issue 13 years ago • 3 comments

CASCADE, SET NULL, and NO ACTION are translated as is. No action defined is - per MySQL documentation actually RESTRICT and thus translated as such. (SET DEFAULT is not accepted by InnoDB)

fvbock avatar Sep 13 '12 04:09 fvbock

just saw that the travis build failed... had a brief look but could not figure out right away whether it's related to my patch or something else... let me know if you'd need me to patch some test code too.

fvbock avatar Sep 13 '12 17:09 fvbock

Travis history shows no single success so this is not you.

kworr avatar Sep 14 '12 12:09 kworr

0kaaay, I had taken some time to study the code and MySQL/PostgreSQL view on standarts.

  1. There are a lot of 'if's in your patch. The code can be wastly simplified, like this:

    for action in ['UPDATE', 'DELETE']:
      match = re.compile(' ON %s (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT)' % action, re.I)
      if match:
        index[action] = 'ON %s %s' % (action, match.group(1))
    

    This is kind of rough example but it saves a lot of ifs and populates index with a final string to output.

    And adding them to the full line regexp would be much better than processing same line with regexp three times. However there is a question whether all MySQL versions generate this line the same way...

  2. Why RESTRICT? NO ACTION is fine default by itself and I see no reasons for changing this. It's not about my preference as I prefer RESTRICT to NO ACTION, but about how MySQL treats triggers and would it enable them alter the data before actual foreign key check is fired. If yes then NO ACTION is closer to MySQL behavior.

kworr avatar Sep 19 '12 13:09 kworr