test that foreign key reflection includes options (on backends with {dialect}.get_foreign_keys() support)
(self, connection, metadata)
| 1005 | |
| 1006 | @testing.only_on(["postgresql", "mysql"]) |
| 1007 | def test_fk_options(self, connection, metadata): |
| 1008 | """test that foreign key reflection includes options (on |
| 1009 | backends with {dialect}.get_foreign_keys() support)""" |
| 1010 | |
| 1011 | if testing.against("postgresql"): |
| 1012 | test_attrs = ( |
| 1013 | "match", |
| 1014 | "onupdate", |
| 1015 | "ondelete", |
| 1016 | "deferrable", |
| 1017 | "initially", |
| 1018 | ) |
| 1019 | addresses_user_id_fkey = sa.ForeignKey( |
| 1020 | # Each option is specifically not a Postgres default, or |
| 1021 | # it won't be returned by PG's inspection |
| 1022 | "users.id", |
| 1023 | name="addresses_user_id_fkey", |
| 1024 | match="FULL", |
| 1025 | onupdate="RESTRICT", |
| 1026 | ondelete="RESTRICT", |
| 1027 | deferrable=True, |
| 1028 | initially="DEFERRED", |
| 1029 | ) |
| 1030 | elif testing.against("mysql"): |
| 1031 | # MATCH, DEFERRABLE, and INITIALLY cannot be defined for MySQL |
| 1032 | # ON UPDATE and ON DELETE have defaults of RESTRICT, which are |
| 1033 | # elided by MySQL's inspection |
| 1034 | addresses_user_id_fkey = sa.ForeignKey( |
| 1035 | "users.id", |
| 1036 | name="addresses_user_id_fkey", |
| 1037 | onupdate="CASCADE", |
| 1038 | ondelete="CASCADE", |
| 1039 | ) |
| 1040 | test_attrs = ("onupdate", "ondelete") |
| 1041 | |
| 1042 | meta = metadata |
| 1043 | Table( |
| 1044 | "users", |
| 1045 | meta, |
| 1046 | Column("id", sa.Integer, primary_key=True), |
| 1047 | Column("name", sa.String(30)), |
| 1048 | test_needs_fk=True, |
| 1049 | ) |
| 1050 | Table( |
| 1051 | "addresses", |
| 1052 | meta, |
| 1053 | Column("id", sa.Integer, primary_key=True), |
| 1054 | Column("user_id", sa.Integer, addresses_user_id_fkey), |
| 1055 | test_needs_fk=True, |
| 1056 | ) |
| 1057 | meta.create_all(connection) |
| 1058 | |
| 1059 | meta2 = MetaData() |
| 1060 | meta2.reflect(connection) |
| 1061 | for fk in meta2.tables["addresses"].foreign_keys: |
| 1062 | ref = addresses_user_id_fkey |
| 1063 | for attr in test_attrs: |
| 1064 | eq_(getattr(fk, attr), getattr(ref, attr)) |