| | 196 | def test_local_and_remote_colnames(self): |
| | 197 | class A(Entity): |
| | 198 | using_options(shortnames=True) |
| | 199 | key1 = Field(Integer, primary_key=True, autoincrement=False) |
| | 200 | key2 = Field(String(40), primary_key=True) |
| | 201 | |
| | 202 | bs_ = ManyToMany('B', local_colname=['foo', 'bar'], |
| | 203 | remote_colname="baz") |
| | 204 | |
| | 205 | class B(Entity): |
| | 206 | using_options(shortnames=True) |
| | 207 | name = Field(String(60)) |
| | 208 | as_ = ManyToMany('A', remote_colname=['foo', 'bar'], |
| | 209 | local_colname="baz") |
| | 210 | |
| | 211 | setup_all(True) |
| | 212 | |
| | 213 | b1 = B(name='b1', as_=[A(key1=10, key2='a1')]) |
| | 214 | |
| | 215 | session.commit() |
| | 216 | session.clear() |
| | 217 | |
| | 218 | a = A.query.one() |
| | 219 | b = B.query.one() |
| | 220 | |
| | 221 | assert a in b.as_ |
| | 222 | assert b in a.bs_ |
| | 223 | |
| | 224 | def test_manual_table_auto_joins(self): |
| | 225 | from sqlalchemy import Table, Column, ForeignKey, ForeignKeyConstraint |
| | 226 | |
| | 227 | a_b = Table('a_b', metadata, |
| | 228 | Column('a_key1', None), |
| | 229 | Column('a_key2', None), |
| | 230 | Column('b_id', None, ForeignKey('b.id')), |
| | 231 | ForeignKeyConstraint(['a_key1', 'a_key2'], |
| | 232 | ['a.key1', 'a.key2'])) |
| | 233 | |
| | 234 | class A(Entity): |
| | 235 | using_options(shortnames=True) |
| | 236 | key1 = Field(Integer, primary_key=True, autoincrement=False) |
| | 237 | key2 = Field(String(40), primary_key=True) |
| | 238 | |
| | 239 | bs_ = ManyToMany('B', table=a_b) |
| | 240 | |
| | 241 | class B(Entity): |
| | 242 | using_options(shortnames=True) |
| | 243 | name = Field(String(60)) |
| | 244 | as_ = ManyToMany('A', table=a_b) |
| | 245 | |
| | 246 | setup_all(True) |
| | 247 | |
| | 248 | b1 = B(name='b1', as_=[A(key1=10, key2='a1')]) |
| | 249 | |
| | 250 | session.commit() |
| | 251 | session.clear() |
| | 252 | |
| | 253 | a = A.query.one() |
| | 254 | b = B.query.one() |
| | 255 | |
| | 256 | assert a in b.as_ |
| | 257 | assert b in a.bs_ |
| | 258 | |
| | 259 | def test_manual_table_manual_joins(self): |
| | 260 | from sqlalchemy import Table, Column, ForeignKey, \ |
| | 261 | ForeignKeyConstraint, and_ |
| | 262 | |
| | 263 | a_b = Table('a_b', metadata, |
| | 264 | Column('a_key1', Integer), |
| | 265 | Column('a_key2', String(40)), |
| | 266 | Column('b_id', String(60))) |
| | 267 | |
| | 268 | class A(Entity): |
| | 269 | using_options(shortnames=True) |
| | 270 | key1 = Field(Integer, primary_key=True, autoincrement=False) |
| | 271 | key2 = Field(String(40), primary_key=True) |
| | 272 | |
| | 273 | bs_ = ManyToMany('B', table=a_b, |
| | 274 | primaryjoin=lambda: and_(A.key1 == a_b.c.a_key1, |
| | 275 | A.key2 == a_b.c.a_key2), |
| | 276 | secondaryjoin=lambda: B.id == a_b.c.b_id, |
| | 277 | foreign_keys=[a_b.c.a_key1, a_b.c.a_key2, |
| | 278 | a_b.c.b_id]) |
| | 279 | |
| | 280 | class B(Entity): |
| | 281 | using_options(shortnames=True) |
| | 282 | name = Field(String(60)) |
| | 283 | |
| | 284 | setup_all(True) |
| | 285 | |
| | 286 | a1 = A(key1=10, key2='a1', bs_=[B(name='b1')]) |
| | 287 | |
| | 288 | session.commit() |
| | 289 | session.clear() |
| | 290 | |
| | 291 | a = A.query.one() |
| | 292 | b = B.query.one() |
| | 293 | |
| | 294 | assert b in a.bs_ |