| 283 | |
| 284 | @testing.variation("use_metadata_reflect", [True, False]) |
| 285 | def test_extend_existing(self, connection, metadata, use_metadata_reflect): |
| 286 | meta = metadata |
| 287 | |
| 288 | Table( |
| 289 | "t", |
| 290 | meta, |
| 291 | Column("id", Integer, primary_key=True), |
| 292 | Column("x", Integer), |
| 293 | Column("y", Integer), |
| 294 | Column("z", Integer, server_default="5"), |
| 295 | ) |
| 296 | meta.create_all(connection) |
| 297 | |
| 298 | m2 = MetaData() |
| 299 | old_z = Column("z", String, primary_key=True) |
| 300 | old_y = Column("y", String) |
| 301 | old_q = Column("q", Integer) |
| 302 | t2 = Table("t", m2, old_z, old_q) |
| 303 | eq_(list(t2.primary_key.columns), [t2.c.z]) |
| 304 | |
| 305 | t2 = Table( |
| 306 | "t", |
| 307 | m2, |
| 308 | old_y, |
| 309 | extend_existing=True, |
| 310 | autoload_with=connection, |
| 311 | ) |
| 312 | if use_metadata_reflect: |
| 313 | m2.reflect(connection, extend_existing=True) |
| 314 | eq_(set(t2.columns.keys()), {"x", "y", "z", "q", "id"}) |
| 315 | |
| 316 | # this has been the actual behavior, the cols are added together, |
| 317 | # however the test wasn't checking this correctly |
| 318 | eq_(list(t2.primary_key.columns), [t2.c.z, t2.c.id]) |
| 319 | |
| 320 | assert t2.c.z is not old_z |
| 321 | if not use_metadata_reflect: |
| 322 | assert t2.c.y is old_y |
| 323 | assert t2.c.z.type._type_affinity is Integer |
| 324 | assert t2.c.q is old_q |
| 325 | |
| 326 | m3 = MetaData() |
| 327 | t3 = Table("t", m3, Column("z", Integer)) |
| 328 | t3 = Table( |
| 329 | "t", |
| 330 | m3, |
| 331 | extend_existing=False, |
| 332 | autoload_with=connection, |
| 333 | ) |
| 334 | if use_metadata_reflect: |
| 335 | m3.reflect(connection, extend_existing=False) |
| 336 | eq_(set(t3.columns.keys()), {"z"}) |
| 337 | |
| 338 | m4 = MetaData() |
| 339 | old_z = Column("z", String, primary_key=True) |
| 340 | old_y = Column("y", String) |
| 341 | old_q = Column("q", Integer) |
| 342 | t4 = Table("t", m4, old_z, old_q) |