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. # Set us on the state.
dict_[self.key] = initiator dict_[self.key] = initiator
# Get the primary key of the initiator and ensure we if initiator is None:
# can support this assignment. # Nullify relationship args
mapper = class_mapper(type(initiator)) dict_[self.parent_token.id.key] = None
if len(mapper.primary_key) > 1: dict_[self.parent_token.discriminator.key] = None
raise sa_exc.InvalidRequestError( else:
'Generic relationships against tables with composite ' # Get the primary key of the initiator and ensure we
'primary keys are not supported.') # 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. # Set the identifier and the discriminator.
discriminator = table_name(initiator) discriminator = table_name(initiator)
dict_[self.parent_token.id.key] = pk dict_[self.parent_token.id.key] = pk
dict_[self.parent_token.discriminator.key] = discriminator dict_[self.parent_token.discriminator.key] = discriminator
class GenericRelationshipProperty(MapperProperty): class GenericRelationshipProperty(MapperProperty):

View File

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