Fix nullify generic relationship

This commit is contained in:
Konsta Vesterinen
2014-01-20 21:30:54 +02:00
parent 6fab2703a1
commit c9b9ec93f9
2 changed files with 21 additions and 12 deletions

View File

@@ -50,20 +50,25 @@ class GenericAttributeImpl(attributes.ScalarAttributeImpl):
# Set us on the state.
dict_[self.key] = initiator
# Get the primary key of the initiator and ensure we
# can support this assignment.
mapper = class_mapper(type(initiator))
if len(mapper.primary_key) > 1:
raise sa_exc.InvalidRequestError(
'Generic relationships against tables with composite '
'primary keys are not supported.')
if initiator is None:
# Nullify relationship args
dict_[self.parent_token.id.key] = None
dict_[self.parent_token.discriminator.key] = None
else:
# Get the primary key of the initiator and ensure we
# can support this assignment.
mapper = class_mapper(type(initiator))
if len(mapper.primary_key) > 1:
raise sa_exc.InvalidRequestError(
'Generic relationships against tables with composite '
'primary keys are not supported.')
pk = mapper.identity_key_from_instance(initiator)[1][0]
pk = mapper.identity_key_from_instance(initiator)[1][0]
# Set the identifier and the discriminator.
discriminator = table_name(initiator)
dict_[self.parent_token.id.key] = pk
dict_[self.parent_token.discriminator.key] = discriminator
# Set the identifier and the discriminator.
discriminator = table_name(initiator)
dict_[self.parent_token.id.key] = pk
dict_[self.parent_token.discriminator.key] = discriminator
class GenericRelationshipProperty(MapperProperty):

View File

@@ -28,6 +28,10 @@ class TestGenericForiegnKey(TestCase):
self.User = User
self.Event = Event
def test_set_as_none(self):
event = self.Event()
event.object = None
def test_set_manual_and_get(self):
user = self.User()