Hi Torsten -

how about we add a standalone function with a guaranteed behavioral
contract, like sqlalchemy.orm.new_instance() or something like that.
it will call class.mapper.new_instance() but new tests in
test/orm/test_session will ensure it maintains the exact behavioral
contract you need.  it can also fail correctly if the class is
unmapped (or perhaps just call __new__), etc.

would you be able to propose a pull req ?



On Fri, Nov 17, 2017 at 8:29 AM, Torsten Landschoff
<[email protected]> wrote:
> Hi Michael et al,
>
>
> tl;dr: I would like ClassManager.new_instance() to be made official as
> discussed in the old discussion and suggest it should initialize the
> polymorphic identity.
>
> Context
> =======
>
> I am not sure if anybody remembers the discussion under
> https://groups.google.com/d/msg/sqlalchemy/8kbu8jL0QII/1dWTRp_DVYYJ
> where I tried to recreate objects not by unpickling but from my own
> serialization code.
>
> With SQLAlchemy I could use MyClass.__new__ to create new instances
> which I would then populate. That failed with SQLAlchemy 0.7 and I
> thought about using ClassManager.new_instance()
>
> Turns out that I never did. Instead my code currently needlessly
> initializes each instance using the __init__ method only to drop that
> information in favor of the deserialized data.
>
> What's worse: __init__ now doesn't initialize new instances, instead the
> code lazily initializes fields before writing to the database or on
> first access via a property - sic!
>
>
> Needless to say I would like to get rid of it. Trying to do so already
> uncovered a bug in the software, where creation times are lost during
> deserialization ending up as the unix epoch.
>
>
> Example code
> ============
>
> Basically I would like to initialize instances like this:
>
> ---------
> mapper = class_mapper(Circle)
> circle = mapper.class_manager.new_instance()
> circle.radius = 42
> session.add(circle)
> session.commit()
> ---------
>
> This fails to initialize the target_type field of Shape (which Circle
> inherits from). Therefore, new_instance works fine for concrete mapped
> classes, unless polymorphism is used.
>
>
> Running the attached example gives this output:
>
>> $ pipenv install
>> [...]
>> $ pipenv run python example.py
>>
>> This works (as expected)
>>
>> This fails (unexpectedly)
>> -> Error is IntegrityError('(sqlite3.IntegrityError) NOT NULL constraint 
>> failed: shape.target_type',)
>>
>> This works (but is a bit ugly)
>
>
> Would you please consider documenting this as a supported use case and
> potentially extend new_instance to set the polymorphic_identity?
>
>
> Thanks a bunch, especially for your hard work in creating SQLAlchemy!
>
> Greetings, Torsten
>
>
> --
> $---+----1----+----2----+----3----+----4----+----5----+----6----+
>
> SCALE GmbH
> Niederlassung Dresden
> Torsten Landschoff
> Pohlandstraße 19
> 01309 Dresden
>
> Tel: +49-351-312002-10
> Fax: +49-351-312002-29
>
> SCALE GmbH
> Registergericht und Sitz: Ingolstadt, HRB 6384
> Geschäftsführer: Dr.-Ing. Heiner Müllerschön, Dipl.-Math. Ulrich Franz
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to