Hi, everyone.
I cannot found information how to find information about new relationships,
when they related with secondary table.
Inspect can't work with class RelationshipProperty.
class_mapper.iterate_properties have't information about changed
attributes
My *task* is:
Write transactions for all changes, i use them for generate old condition
of instances
I try to use:
Write to common table Transcations, data:
classname,
instance_id,
attribute_name,
new_value,
old_value
TransactionsComment - group of transactions (such as *git commit*)
*Example* is:
users=db.session.query(Group_).first()
if users is None:
users=Group_(name='users')
db.session.add(users)
db.session.commit()
print ('now on user')
##New User
user=db.session.query(User_).first()
if user is None:
# print ('need new user')
user=User_(login='trololo', groups=[users])
db.session.add(user)
db.session.commit()
return 'good'
I use *listener "before_commit"*
#update
for i in db.session.dirty:
tr=[]
print ('\nUpdated '+i.__class__.__name__+' '+str(i.__dict__))
for prop in class_mapper(i.__class__).iterate_properties:
if isinstance(prop, RelationshipProperty):
if prop.parent==i.__mapper__:
print (str(prop.key))
print ('try to find '+prop.key+' in '+str(i.__dict__))
if prop.key in i.__dict__:
print (inspect(i.__dict__[prop.key]))
# for rel_attr in inspect(prop).attrs:
# if rel_attr.history.has_changes:
# print (str(rel_attr))
#create
for i in db.session.new:
print ('\nNew '+i.__class__.__name__+' '+str(i.__dict__))
if getattr(i,'Document',False) is not False:
doc=db.session.query(Document_).first()
if doc is None:
doc=Document_(name='test', extension='txt')
print ('added document property to'+str(i.__dict__))
i.Document=doc
*Result* is
New Group_ {'name': 'users', 'polymorphic_type': 'group_',
'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at
0x7f9854eee630>}
added document property to{'_sa_instance_state':
<sqlalchemy.orm.state.InstanceState object at 0x7f9854eee630>, 'name':
'users', 'mRID': UUID('1c0a2e32-20b9-4ecf-af62-5cf919e16269'),
'polymorphic_type': 'group_'}
now on user
Updated Group_ {'_sa_instance_state':
<sqlalchemy.orm.state.InstanceState object at 0x7f9854eee630>}
users
try to find users in {'_sa_instance_state':
<sqlalchemy.orm.state.InstanceState object at 0x7f9854eee630>}
childrens
try to find childrens in {'_sa_instance_state':
<sqlalchemy.orm.state.InstanceState object at 0x7f9854eee630>}
parents
try to find parents in {'_sa_instance_state':
<sqlalchemy.orm.state.InstanceState object at 0x7f9854eee630>}
New User_ {'login': 'trololo', 'groups': True, '_sa_instance_state':
<sqlalchemy.orm.state.InstanceState object at 0x7f9854eee978>,
'polymorphic_type': 'user_'}
added document property to{'login': 'trololo', 'groups': True,
'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at
0x7f9854eee978>, 'mRID': UUID('c07fe890-8bb4-4b36-abe6-afd85148d005'),
'polymorphic_type': 'user_'}
Thanks for read my answer, if you have minds how i can find this changes,
or another path to complete this task i would like to read them
--
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.