Re: [sqlalchemy] delete of polymorphic objects leaves orphaned parent objects

2019-10-15 Thread natsjoo sodillepa
@Mike @Simon,

Thanks guys,

I fully understand what you need. However I'm currently very, very busy and 
just don't have the time.
The reason I posted without providing what you need was a little hope that 
maybe somebody
recognized a similar problem.
We have around 120 objects and trimming down to the root of the evil will 
take some time. I will
try to do so and come back here if I don't get it.

Cya,
Nacho

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/5d11b8db-e588-47de-ba4b-5dde94f81bc9%40googlegroups.com.


Re: [sqlalchemy] delete of polymorphic objects leaves orphaned parent objects

2019-10-14 Thread natsjoo sodillepa
Ok,

I have managed to get rid of the problem, but I'm don't like what is 
happening.
Solution: remove a secondary relation from the model:

model = relationship("Model", secondary='item_group', uselist=False)


After this, the following code does do a proper delete of the items:

for it in self.session.query(ItemMeta).filter_by(item_group=item_group).all():
if type(it) == ItemMeta:
item_group.items.remove(it)
self.session.delete(it)
self.session.commit()


However, things are still not ideal. The following should work but doesn't:

for item in item_group.items:
   self.session.delete(item)

Then only half of the item get deleted, which is a bit weird.

Anybody any thoughts on this? Why does a secondary relation of this kind of 
effect on a delete?

Kinde regards,
Nacho

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/073b7947-ed51-4b63-bfdb-a5499128c06c%40googlegroups.com.


Re: [sqlalchemy] delete of polymorphic objects leaves orphaned parent objects

2019-10-11 Thread natsjoo sodillepa
Hi Simon,
Thank you for your reply. 
I've tried all the variant I could think of, also eg for item in items: 
session.delete(it),
with and without remove on the list of items, all known mapper and relation 
params but nothing.

However, I've tried a similar structure from examples from the web and 
those seems to work,
so the problem must lie somewhere in our code. Or could it have anything to 
do with scoped sessions?

Anyway: I'm working on the isolation of the problem, which is not that 
easy. I'll come back here if I've
got something more MCVE or with a solution.

Kind regards,
Nacho

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/32d41bf0-11a2-4552-9ae3-450808074080%40googlegroups.com.


[sqlalchemy] delete of polymorphic objects leaves orphaned parent objects

2019-10-11 Thread natsjoo sodillepa
Hi all,

We have a list of polymorphic objects from which delete object does not 
work, not matter what we try.
The situation:

class ItemGroup(Base):
__tablename__ = 'item_group'
__table_args__ = (
UniqueConstraint('model_id', 'item_group_color_id', 
name='unique_model_id_item_group_color_id_uc'),
)

id = Column(Integer, primary_key=True)
items = relationship("Item", back_populates="item_group")

class Item(Base):
__tablename__ = 'item'

id = Column(Integer, primary_key=True)

item_group_id = Column(ForeignKey('item_group.id'), nullable=False, 
index=True)
item_group = relationship('ItemGroup', back_populates="items", 
uselist=False)

__mapper_args__ = {
'polymorphic_identity': __tablename__,
'polymorphic_on': item_type
}

class ItemMeta(Item):
__tablename__ = 'item_meta'

id = Column(Integer, ForeignKey('item.id', ondelete="CASCADE"), 
primary_key=True)

meta_name = Column(String(255, collation), nullable=False)

__mapper_args__ = {
'polymorphic_identity': __tablename__,
}


The problem occurs after a delete:

  session.query(ItemMeta).filter_by(item_group=ig).delete()

Now, querying the child works fine: 
  session.query(ItemMeta).filter_by(item_group=ig).all()
  []

But querying the parent:
  test_fixtures.session.query(Item).filter_by(item_group=ig).all()
  

  Give: Instance '' has been deleted, or its row is 
otherwise not present.

In the database I can see the lingering parent objects. I guess that I have to 
use something like
delete-orphan, but as I dont have a relation from child to father, so now I'm 
stuck.

Any ideas are welcome.

Kind regards,
Nacho

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/7dea2eaa-6390-4a54-abd5-fae925727c17%40googlegroups.com.


Re: [sqlalchemy] "No such polymorphic_identity" when models in different modules

2019-07-08 Thread natsjoo sodillepa

Got it.

As I like to "eat the pudding":
x.py:

class Meta(type):
   print("Here is Meta")
   def __new__(cls, name, bases, dct):
  print("meta.new")
  return super().__new__(cls, name, bases, dct)

class Foo(metaclass=Meta):
   print("Here is Foo")


y.py:

from x import Foo

class What(Foo):
   print("Here's What, a Foo")


Giving as output on python y.py:
Here is Meta
Here is foo
meta.new
Here's What, a Foo
meta.new

Showing that the meta class indeed is called on construction of "What": 
hence there you have the mechanics of class registration.

Now that's covered I've to say I'm not too happy importing module's with no 
apparent use, but that seems the nature of the beast.

Kind regards,
Nacho

 

> The pattern from a software architecture point of view was identified by 
> Martin Fowler as the "registry" pattern, 
> https://martinfowler.com/eaaCatalog/registry.html, however this pattern 
> doesn't say much about the mechanics used.The registry pattern refers 
> to linking the creation of a class or object to its automatic inclusion in 
> some semi-global or in some cases global registry, which is one SQLAlchemy 
> uses a lot, in some cases explicitly and in other cases only behind the 
> scenes.
>
> The declarative metaclass contains a function which scans your class for 
> attributes, builds a Table from these attributes which is associated with 
> the MetaData collection that the metaclass has access towards (this is 
> registry pattern #1).  Then, the mapper() function is invoked against the 
> class you created along with this Table, which creates a new Mapper object 
> that is added to a global, weak-referencing collection inside the 
> sqlalchemy.orm.mapper module (this is registry pattern #2).  When the 
> mapper "inherits" from another one as seems to be the case here, that 
> inheriting mapper is also amended to include this new mapper in its 
> collection of subclasses.
>
> The overall "mapper configure" step which you may have seen scans through 
> this registry of mapper objects and makes sure all the mappers that refer 
> to each other, usually through the relationship() linkage, are fully linked 
> and have been found.
>
>
>

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/bac20779-de45-4889-9a03-25f0d4fd20b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "No such polymorphic_identity" when models in different modules

2019-07-05 Thread natsjoo sodillepa
For the curious. One thing that lingers around is the question: how does 
SQLalchemy make the connection between the parent and the child classes?

In the separate modules, where the child classes are defined there is no 
other code. The only thing that gets executed is the class construction, 
but as
far as I know that doesn't trigger execution of code in the parent class. I 
know that SQLalchemy uses a metaclass but that's used only for
instantiation of instances, not of classes, or is it?

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/c90cafee-13a4-4759-99a7-b2fd656697b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "No such polymorphic_identity" when models in different modules

2019-07-04 Thread natsjoo sodillepa
@MIke
 

> SQLAlchemy has to know about all the classes before you do a query that's 
> going to refer to a remote class' polymorphic_identity, so somewhere you 
> have to make sure the module was imported.
>
>
I imported the subclass and indeed now "it works". At first this is counter 
intuitive, but I understand the need.
The point now is that I don't really want to know what subclasses there are 
(hence the polymorfism), so now
I've to find a way to import all relevant modules. I'll figure this out but 
any ideas are welcome.

Anyway: thanks both Mike & Simon for pointing this out.

Nacho

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/de8cd30f-f0b6-479d-a823-e65bb32a1c1b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "No such polymorphic_identity" when models in different modules

2019-07-04 Thread natsjoo sodillepa
@Simon.
Interesting point. I create the DB and the instances in one script. The 
data in the db seems to be correct.
However the error occurs in a second script which almost is like this:

import ClassA2

... create session ...

objectA2 = session.query(ClassA2).first()

for objectA1 in objectA2.list_objectA1:
objectA1.do_something()

So, no here I don't import ClassA1, but I shouldn't, should I (?).

The error occurs on entering the for loop.


El jueves, 4 de julio de 2019, 13:16:12 (UTC+2), Simon King escribió:
>
> SQLAlchemy doesn't care if your classes are defined in a single file 
> or multiple files. 
>
> When you got the error, is it possible that you hadn't imported 
> moduleB? If you haven't imported it, SQLAlchemy will have no idea that 
> ClassB exists. 
>
> Simon 
>
> On Thu, Jul 4, 2019 at 11:53 AM natsjoo sodillepa  > wrote: 
> > 
> > Hi all, 
> > 
> > I got an "No such polymorphic_identity" error in the following 
> situation: 
> > - I use Declerative and joined table polymorfism style 
> > - moduleA defines Base, and a lot of Classes, one of them ClassA1(Base) 
> and ClassA2(Base) 
> > - moduleB contains a subclassed ClassB(ClassA1) 
> > - ClassA2 contains a 1:n relation with ClassA1 
> > 
> > when I try to get the list of ClassA1 objects of ClassA2 I get a "No 
> such polymorphic_identity" 
> > error. 
> > 
> > However, if I put everything in the same file things work fine. 
> > 
> > So my question is: can I put subclasses in different modules and if so: 
> how? 
> > 
> > Kind regards, 
> > Nacho 
> > 
> > -- 
> > 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 sqlal...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/53a77b0e-b28c-4c1d-a54a-ffac09cfb5be%40googlegroups.com.
>  
>
> > 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/a4af5e8f-d17a-4fd8-a0d4-5fb6a058b998%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] "No such polymorphic_identity" when models in different modules

2019-07-04 Thread natsjoo sodillepa
Hi all,

I got an "No such polymorphic_identity" error in the following situation:
- I use Declerative and joined table polymorfism style
- moduleA defines Base, and a lot of Classes, one of them ClassA1(Base) and 
ClassA2(Base)
- moduleB contains a subclassed ClassB(ClassA1)
- ClassA2 contains a 1:n relation with ClassA1

when I try to get the list of ClassA1 objects of ClassA2 I get a "No such 
polymorphic_identity"
error.

However, if I put everything in the same file things work fine.

So my question is: can I put subclasses in different modules and if so: how?

Kind regards,
Nacho

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/53a77b0e-b28c-4c1d-a54a-ffac09cfb5be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.