[email protected] wrote:

> On 2015-03-08 11:17 Michael Bayer <[email protected]> wrote:
>> new_obj = MyClass()
>> for attr in mapper.attrs:
>>    setattr(new_obj, attr.key, getattr(old_obj, attr.key))
> 
> This would copy everything including primary keys and unique members.

which is why, “copy an object” is not an out of the box thing. Because
nobody really wants a full “copy”, it’s something custom.

> I have hard problems with the SQLA-docu. I know that 'attr' is from
> type 'class ColumnProperty'. But I am not able to found out the members
> of it. I can not see a 'primary_key' or a 'unique' member inside of it.
> 
> How can I "ask" if it is a primary key or unique?

mapper = inspect(MyClass)

is_pk = mapper.attrs.somekey.columns[0].primary_key

for “unique”, there are multiple definitions for “unique”. Do you consider a
column to be “unique” if it is part of a composite unique constraint or
index (e.g. has other columns)? I don’t know. Let’s assume you do:

for constraint in mapper.local_table.constraints:
    if isinstance(constraint, UniqueConstraint):
        if constraint.columns.contains_column(mapper.attrs.somekey.columns[0]):
            is_unique = True

for index in mapper.local_table.indexes:
   if index.unique and 
index.columns.contains_column(mapper.attrs.somekey.columns[0]):
       is_unique = True


> 
> And much better for the future: How do I have to use the SQLA-docu to
> solve such problems myself? What do I understand wrong in that docu?

use the search feature and familiarize with all the public accessors on the
handful of objects that are always involved in these kinds of things, Table,
Mapper, ColumnProperty, ColumnCollection, Constraint (that last one is missing, 
will add).





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

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

Reply via email to