--- /dev/null
+.. change::
+ :tags: sql, reflection
+ :tickets: 4741
+
+ The "NO ACTION" keyword for foreign key "ON UPDATE" is now considered to be
+ the default cascade for a foreign key on all supporting backends (SQlite,
+ MySQL, PostgreSQL) and when detected is not included in the reflection
+ dictionary; this is already the behavior for PostgreSQL and MySQL for all
+ previous SQLAlchemy versions in any case. The "RESTRICT" keyword is
+ positively stored when detected; PostgreSQL does report on this keyword,
+ and MySQL as of version 8.0 does as well. On earlier MySQL versions, it is
+ not reported by the database.
con_kw = {}
for opt in ("onupdate", "ondelete"):
- if spec.get(opt, False):
+ if spec.get(opt, False) not in ("NO ACTION", None):
con_kw[opt] = spec[opt]
fkey_d = {
#
# unique constraints come back as KEYs
kw = quotes.copy()
- kw["on"] = "RESTRICT|CASCADE|SET NULL|NOACTION"
+ kw["on"] = "RESTRICT|CASCADE|SET NULL|NO ACTION"
self._re_fk_constraint = _re_compile(
r" "
r"CONSTRAINT +"
preparer._unquote_identifier(x)
for x in re.split(r"\s*,\s", referred_columns)
]
+ options = {
+ k: v
+ for k, v in [
+ ("onupdate", onupdate),
+ ("ondelete", ondelete),
+ ("initially", initially),
+ ("deferrable", deferrable),
+ ("match", match),
+ ]
+ if v is not None and v != "NO ACTION"
+ }
fkey_d = {
"name": conname,
"constrained_columns": constrained_columns,
"referred_schema": referred_schema,
"referred_table": referred_table,
"referred_columns": referred_columns,
- "options": {
- "onupdate": onupdate,
- "ondelete": ondelete,
- "deferrable": deferrable,
- "initially": initially,
- "match": match,
- },
+ "options": options,
}
fkeys.append(fkey_d)
return fkeys
for token in re.split(r" *\bON\b *", onupdatedelete.upper()):
if token.startswith("DELETE"):
- options["ondelete"] = token[6:].strip()
+ ondelete = token[6:].strip()
+ if ondelete and ondelete != "NO ACTION":
+ options["ondelete"] = ondelete
elif token.startswith("UPDATE"):
- options["onupdate"] = token[6:].strip()
+ onupdate = token[6:].strip()
+ if onupdate and onupdate != "NO ACTION":
+ options["onupdate"] = onupdate
yield (
constraint_name,
constrained_columns,
def foreign_key_constraint_option_reflection_ondelete(self):
return exclusions.closed()
+ @property
+ def fk_constraint_option_reflection_ondelete_restrict(self):
+ return exclusions.closed()
+
+ @property
+ def fk_constraint_option_reflection_ondelete_noaction(self):
+ return exclusions.closed()
+
@property
def foreign_key_constraint_option_reflection_onupdate(self):
return exclusions.closed()
+ @property
+ def fk_constraint_option_reflection_onupdate_restrict(self):
+ return exclusions.closed()
+
@property
def temp_table_reflection(self):
return exclusions.open()
def test_get_foreign_key_options_onupdate(self):
self._test_get_foreign_key_options(onupdate="SET NULL")
+ @testing.requires.foreign_key_constraint_option_reflection_onupdate
+ def test_get_foreign_key_options_onupdate_noaction(self):
+ self._test_get_foreign_key_options(onupdate="NO ACTION", expected={})
+
+ @testing.requires.fk_constraint_option_reflection_ondelete_noaction
+ def test_get_foreign_key_options_ondelete_noaction(self):
+ self._test_get_foreign_key_options(ondelete="NO ACTION", expected={})
+
+ @testing.requires.fk_constraint_option_reflection_onupdate_restrict
+ def test_get_foreign_key_options_onupdate_restrict(self):
+ self._test_get_foreign_key_options(onupdate="RESTRICT")
+
+ @testing.requires.fk_constraint_option_reflection_ondelete_restrict
+ def test_get_foreign_key_options_ondelete_restrict(self):
+ self._test_get_foreign_key_options(ondelete="RESTRICT")
+
@testing.provide_metadata
- def _test_get_foreign_key_options(self, **options):
+ def _test_get_foreign_key_options(self, expected=None, **options):
meta = self.metadata
+ if expected is None:
+ expected = options
+
Table(
"x",
meta,
eq_(dict((k, opts[k]) for k in opts if opts[k]), {})
opts = insp.get_foreign_keys("user")[0]["options"]
- eq_(dict((k, opts[k]) for k in opts if opts[k]), options)
+ eq_(opts, expected)
+ # eq_(dict((k, opts[k]) for k in opts if opts[k]), expected)
def _assert_insp_indexes(self, indexes, expected_indexes):
index_names = [d["name"] for d in indexes]
"referred_columns": ["id"],
"referred_table": "industry",
"referred_schema": None,
- "options": {
- "onupdate": "CASCADE",
- "deferrable": None,
- "ondelete": "CASCADE",
- "initially": None,
- "match": None,
- },
+ "options": {"onupdate": "CASCADE", "ondelete": "CASCADE"},
},
}
metadata.create_all()
"referred_schema": None,
"name": "fk4",
"constrained_columns": ["c4"],
- "options": {"onupdate": "NO ACTION"},
+ "options": {},
},
],
)
def foreign_key_constraint_option_reflection_ondelete(self):
return only_on(["postgresql", "mysql", "sqlite", "oracle"])
+ @property
+ def fk_constraint_option_reflection_ondelete_restrict(self):
+ return only_on(["postgresql", "sqlite", self._mysql_80])
+
+ @property
+ def fk_constraint_option_reflection_ondelete_noaction(self):
+ return only_on(["postgresql", "mysql", "sqlite"])
+
@property
def foreign_key_constraint_option_reflection_onupdate(self):
return only_on(["postgresql", "mysql", "sqlite"])
+ @property
+ def fk_constraint_option_reflection_onupdate_restrict(self):
+ return only_on(["postgresql", "sqlite", self._mysql_80])
+
@property
def comment_reflection(self):
return only_on(["postgresql", "mysql", "oracle"])
return only_if(check)
+ def _mysql_80(self, config):
+ return (
+ against(config, "mysql")
+ and config.db.dialect._is_mysql
+ and config.db.dialect.server_version_info >= (8,)
+ )
+
def _mariadb_102(self, config):
return (
against(config, "mysql")