Re: [sqlalchemy] How to get ForeignKey from Relationship property

2018-04-11 Thread Tolstov Sergey
Thanks for answer.
I dynamicly create classes, their columns, their relationship.
Add new works with new_instances, who will load after defintion, and 
already loaded classes who doesn't contain foreignkeys(for relationships).
session.expire(attribute), does not work, because contained foreign key 
does not refreshed, and sqlalchemy think that is equal None

Kludge is look for adding foreign_keys and refresh them

if refreshed_attribute is not None:
  for instance in gc.get_objects():
if isinstance(instance, refreshed_attribute['factory_class']):
  if instance in session.dirty or instance in session.deleted:
session.refresh(current_instance, attribute_names = 
[refreshed_attribute['refreshed_attribute']])
  elif instance in session and instance not in session.new:
session.refresh(instance) 

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] mapping a class against multiple tables + relationship() .. ?

2018-04-11 Thread Mike Bayer
On Wed, Apr 11, 2018 at 6:15 AM, Julien Cigar  wrote:
> On Tue, Apr 10, 2018 at 11:53:09AM -0400, Mike Bayer wrote:
>> On Tue, Apr 10, 2018 at 9:28 AM, Julien Cigar  wrote:
>> > Hello,
>> >
>> > I wondered if it is possible to use a class mapped against multiple
>> > tables as a relationship() in another class?
>>
>> it is, there's examples at
>> http://docs.sqlalchemy.org/en/latest/orm/join_conditions.html#relationship-to-non-primary-mapper
>>
>> that is map your related class to whatever you want in a non primary
>> mapper.   If you are just selecting data you have a lot of options.
>>
>
> Hi Mike,
>
> Thanks for the link :) I tried and it seems to work,  except in an
> inheritance scenario (1). I guess that in a non primary mapper I can't
> put my polymorphic_on condition on a Join clause?
>
> (1) https://gist.github.com/silenius/a237baf8c4bcd79550dc884f2eeb1998

this is a lot of code to follow, but if the point of the mutli-table
thing you're doing is to game the joined inheritance into doing
something, then that's probably not the appropriate use for it.   It
looks like you're trying to add all kinds of columns to
"ContentTranslation" from the other mappers like Content.


>
> Thanks!
> Julien
>
>>
>> >
>> > something like:
>> >
>> > ### Content
>> >
>> > content_translation_join = sql.join(
>> >   t_content, t_content_translation
>> > )
>> >
>> > orm.mapper(
>> >   ContentTranslation, content_translation_join,
>> >   polymorphic_on=t_content.c.content_type_id
>> > )
>> >
>> > orm.mapper(
>> >   Content, t_content,
>> >   polymorphic_on=t_content.c.content_type_id,
>> >   properties={
>> > 'translations': orm.relationship(
>> >   ContentTranslation,
>> >   # more stuff here
>> > )
>> >   }
>> > )
>> >
>> > ### Document
>> >
>> > orm.mapper(
>> >   DocumentTranslation, t_document_translation,
>> >   inherits=ContentTranslation,
>> >   polymorphic_identity=some_id
>> > )
>> >
>> > orm.mapper(
>> >   Document, t_document,
>> >   inherits=Content,
>> >   polymorphic_identity=some_id
>> > )
>> >
>> > I tried (1) but it doesn't seems to work so I want to be sure that it's
>> > not possible :)
>> >
>> > Thanks!
>> > Julien
>> >
>> > (1) 
>> > https://gist.github.com/silenius/561b13f4b987c36434cd81e2c08cab6e#file-foo-py
>> >
>> >
>> >
>> > --
>> > Julien Cigar
>> > Belgian Biodiversity Platform (http://www.biodiversity.be)
>> > PGP fingerprint: EEF9 F697 4B68 D275 7B11  6A25 B2BB 3710 A204 23C0
>> > No trees were killed in the creation of this message.
>> > However, many electrons were terribly inconvenienced.
>> >
>> > --
>> > 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.
>> > 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.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> Julien Cigar
> Belgian Biodiversity Platform (http://www.biodiversity.be)
> PGP fingerprint: EEF9 F697 4B68 D275 7B11  6A25 B2BB 3710 A204 23C0
> No trees were killed in the creation of this message.
> However, many electrons were terribly inconvenienced.
>
> --
> 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.
> For more options, visit https://groups.google.com/

Re: [sqlalchemy] mapping a class against multiple tables + relationship() .. ?

2018-04-11 Thread Julien Cigar
On Tue, Apr 10, 2018 at 11:53:09AM -0400, Mike Bayer wrote:
> On Tue, Apr 10, 2018 at 9:28 AM, Julien Cigar  wrote:
> > Hello,
> >
> > I wondered if it is possible to use a class mapped against multiple
> > tables as a relationship() in another class?
> 
> it is, there's examples at
> http://docs.sqlalchemy.org/en/latest/orm/join_conditions.html#relationship-to-non-primary-mapper
> 
> that is map your related class to whatever you want in a non primary
> mapper.   If you are just selecting data you have a lot of options.
> 

Hi Mike,

Thanks for the link :) I tried and it seems to work,  except in an 
inheritance scenario (1). I guess that in a non primary mapper I can't 
put my polymorphic_on condition on a Join clause?

(1) https://gist.github.com/silenius/a237baf8c4bcd79550dc884f2eeb1998

Thanks!
Julien

> 
> >
> > something like:
> >
> > ### Content
> >
> > content_translation_join = sql.join(
> >   t_content, t_content_translation
> > )
> >
> > orm.mapper(
> >   ContentTranslation, content_translation_join,
> >   polymorphic_on=t_content.c.content_type_id
> > )
> >
> > orm.mapper(
> >   Content, t_content,
> >   polymorphic_on=t_content.c.content_type_id,
> >   properties={
> > 'translations': orm.relationship(
> >   ContentTranslation,
> >   # more stuff here
> > )
> >   }
> > )
> >
> > ### Document
> >
> > orm.mapper(
> >   DocumentTranslation, t_document_translation,
> >   inherits=ContentTranslation,
> >   polymorphic_identity=some_id
> > )
> >
> > orm.mapper(
> >   Document, t_document,
> >   inherits=Content,
> >   polymorphic_identity=some_id
> > )
> >
> > I tried (1) but it doesn't seems to work so I want to be sure that it's
> > not possible :)
> >
> > Thanks!
> > Julien
> >
> > (1) 
> > https://gist.github.com/silenius/561b13f4b987c36434cd81e2c08cab6e#file-foo-py
> >
> >
> >
> > --
> > Julien Cigar
> > Belgian Biodiversity Platform (http://www.biodiversity.be)
> > PGP fingerprint: EEF9 F697 4B68 D275 7B11  6A25 B2BB 3710 A204 23C0
> > No trees were killed in the creation of this message.
> > However, many electrons were terribly inconvenienced.
> >
> > --
> > 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.
> > 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
Julien Cigar
Belgian Biodiversity Platform (http://www.biodiversity.be)
PGP fingerprint: EEF9 F697 4B68 D275 7B11  6A25 B2BB 3710 A204 23C0
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: PGP signature


Re: [sqlalchemy] How to get ForeignKey from Relationship property

2018-04-11 Thread Simon King
On Wed, Apr 11, 2018 at 7:40 AM, Tolstov Sergey  wrote:
> I use dynamic constructor for class.
> It works fine for instance who have not foreign keys. But when FK on another
> instance it cannot load them
>
> Example:
> class left (Base):
>   id = sqlalchemy.Column(UUID, primary_key = True)
>   def __getattr__(self, attribute):
> if attribute == 'rights':
>   right().left
>   return getattr(self, attribute)
>
> class right(Base):
>   id = sqlalchemy.Column(UUID, primary_key = True)
>   def __getattr__(self, attribute):
> if attribute == 'left':
>   rel_key = sqlalchemy.Column(UUID, sqlalchemy.ForeignKey(left.mRID),
> nullable = True)
>   setattr(self.__class__, 'left_mRID', rel_key)
>   setattr(self.__class__, 'left', sqlalchemy.orm.relationship(left,\
>foreign_keys = rel_key, uselist = False,\
>backref = sqlalchemy.orm.backref('rights', uselist = True)))
>   return getattr(self,attribute)
>
> Works:
> left_1 = session.query(left).first()
> print (str(left_1.rights))
>
> Not works:
> right_1 = session.query(right).first()
> print (str(right_1.left))
> I think that it may be fix them, but i need universal for all
> relationshipproperties
> class right(Base):
>   ...
>   def __getattr__(self, attribute):
> ...
> session.refresh(self, attribute_names = ['left_mRID'])
>

I don't know what's going on here, but this isn't really the way
SQLAlchemy is expected to work. Columns and relationships are normally
set up statically, and SQLAlchemy has a "compile mappers" phase which
figures out the connections between all the classes, which you might
be bypassing by defining them dynamically.

Can you explain why you are trying to create columns and relationships
dynamically? There might be a different way to approach the problem.

Simon

-- 
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.
For more options, visit https://groups.google.com/d/optout.