[sqlalchemy] How to handle database events?

2018-09-17 Thread uralbash
Hello, I need to track events that come from the FireBird database 
(https://www.firebirdsql.org/file/documentation/reference_manuals/driver_manuals/odbc/html/fbodbc205-events.html).
 
Is it possible to do this with SQLAlchemy?

-- 
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] Re: Representing Tree Structures with sqlAlchemy

2017-07-22 Thread uralbash
You can also use the library https://github.com/uralbash/sqlalchemy_mptt


пятница, 21 июля 2017 г., 20:40:57 UTC+5 пользователь Nathan Mooth написал:
>
> So I have a tree-like data structure that I need to store in a way so that 
> I can see all children or all parents of a query as well as their relative 
> depth from the query item. In the past I have been using the closure table 
> method demonstrated in this blog post 
> <http://dirtsimple.org/2010/11/simplest-way-to-do-tree-based-queries.html>, 
> however now that I am switching to sqlAlchemy I am wondering if there is a 
> better way to do this. So basically I am trying to figure out how to run a 
> series of inserts on my closure table whenever an item is added to the main 
> table, as well as what would be the best way to load the child or parent 
> items of a query.
>

-- 
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] How to reflect database with auto quote_name of __tablename__ ?

2016-11-24 Thread uralbash
Yes thank you, indeed everything works.
I just made the wrong logic of migration.

-- 
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] How to reflect database with auto quote_name of __tablename__ ?

2016-11-16 Thread uralbash
Thank you, now there are no errors and SQLAlchemy works fine but probably 
alembic do it wrong.

When I created first migration it doesn't detect qouted (case sensitive) 
tables like "doctor" 
(https://gist.github.com/uralbash/e83fef54003cef3111d9d4cd18145708#file-alembic-bash)
 

and not quoted tables added twice in migration file 
(https://gist.github.com/uralbash/e83fef54003cef3111d9d4cd18145708#file-2bcdb50b589a_create_doctors_patients_appointments-py)

INFO  [alembic.autogenerate.compare] Detected added table 'PATIENTS'
INFO  [alembic.autogenerate.compare] Detected removed table 'patients'

I tested it a few times and create everything from scratch 
(https://gist.github.com/uralbash/e83fef54003cef3111d9d4cd18145708#file-db_create-sql)
 
but always I get this wrong result

среда, 16 ноября 2016 г., 19:30:34 UTC+5 пользователь Mike Bayer написал:
>
> still trying to get fdb to work but looking at the source it seems like 
> https://bitbucket.org/zzzeek/sqlalchemy/issues/3548 needs to be ported 
> to firebird, as the steps taken for Oracle weren't replicated.   should 
> be easy if this is the case. 
>
>
>
> On 11/16/2016 01:31 AM, uralbash wrote: 
> > Thank you, I have prepared a simple example with three tables 
> > https://gist.github.com/uralbash/a623e621093a6a10fd2ea85b5a1ee124 
> > 
> > To avoid install FireBird in my system I use Docker + Vagrant 
> > 
> https://github.com/uralbash/docker-template/blob/master/vagrant/databases/firebird/Vagrantfile
>  
> > and GUI client FlameRobin 
> > 
> > понедельник, 14 ноября 2016 г., 20:19:52 UTC+5 пользователь Mike Bayer 
> > написал: 
> > 
> > 
> > 
> > On 11/14/2016 01:43 AM, uralbash wrote: 
> > > I use quoted_name to describe the table schema (declarative 
> > method) in 
> > > my project like this: 
> > > 
> > > | 
> > > classPeople(Base): 
> > > """ 
> > > .. restapi:: 
> > > :table: people 
> > > """ 
> > > __tablename__ =quoted_name('people',quote=True) 
> > > 
> > > id =Column( 
> > > quoted_name("id",quote=True), 
> > > Integer,Sequence('GEN_people_ID'), 
> > > primary_key=True,autoincrement=True 
> > > ) 
> > > name =Column( 
> > > quoted_name("name",quote=True), 
> > > Unicode 
> > > ) 
> > > | 
> > > 
> > > And it's work ok for me. 
> > > Now I want to make migration for other FireBird database with auto 
> > > reflect table, because it designed outside of python and 
> SQLAlchemy. 
> > > I redid env.py as you suggested. At first I get all tablenames by 
> raw 
> > > query and then autoload table and put it to metadata. 
> > > 
> > > | 
> > > # future 
> > > from__future__ importwith_statement 
> > > 
> > > # standard library 
> > > fromlogging.config importfileConfig 
> > > 
> > > # SQLAlchemy 
> > > importsqlalchemy 
> > > fromsqlalchemy importTable,MetaData,pool,engine_from_config 
> > > fromsqlalchemy.ext.automap importautomap_base 
> > > fromsqlalchemy.sql.elements importquoted_name 
> > > fromsqlalchemy.ext.declarative importdeclarative_base 
> > > 
> > > # third-party 
> > > fromalembic importcontext 
> > > fromalembic.ddl.impl importDefaultImpl 
> > > 
> > > 
> > > classFirebirdImpl(DefaultImpl): 
> > > __dialect__ ='firebird' 
> > > transactional_ddl =True 
> > > 
> > > 
> > > # this is the Alembic Config object, which provides 
> > > # access to the values within the .ini file in use. 
> > > config =context.config 
> > > 
> > > metadata =MetaData() 
> > > 
> > > engine =engine_from_config( 
> > > config.get_section(config.config_ini_section), 
> > > prefix='sqlalchemy.', 
> > > poolclass=pool.NullPool) 
> > > 
> > > q =engine.execute(''' 
> > > select rdb$relation_name 
> > > from rdb$relations 
> > > where rdb$view_blr is null 
> > > and (rdb$system_flag is null or rdb$system_flag = 0) 
> > > ''') 
> > > tables =[x[0].strip()forx inq.fetchall()] 
> &g

Re: [sqlalchemy] How to reflect database with auto quote_name of __tablename__ ?

2016-11-15 Thread uralbash
Thank you, I have prepared a simple example with three tables 
https://gist.github.com/uralbash/a623e621093a6a10fd2ea85b5a1ee124

To avoid install FireBird in my system I use Docker + Vagrant 
https://github.com/uralbash/docker-template/blob/master/vagrant/databases/firebird/Vagrantfile
 
and GUI client FlameRobin

понедельник, 14 ноября 2016 г., 20:19:52 UTC+5 пользователь Mike Bayer 
написал:
>
>
>
> On 11/14/2016 01:43 AM, uralbash wrote: 
> > I use quoted_name to describe the table schema (declarative method) in 
> > my project like this: 
> > 
> > | 
> > classPeople(Base): 
> > """ 
> > .. restapi:: 
> > :table: people 
> > """ 
> > __tablename__ =quoted_name('people',quote=True) 
> > 
> > id =Column( 
> > quoted_name("id",quote=True), 
> > Integer,Sequence('GEN_people_ID'), 
> > primary_key=True,autoincrement=True 
> > ) 
> > name =Column( 
> > quoted_name("name",quote=True), 
> > Unicode 
> > ) 
> > | 
> > 
> > And it's work ok for me. 
> > Now I want to make migration for other FireBird database with auto 
> > reflect table, because it designed outside of python and SQLAlchemy. 
> > I redid env.py as you suggested. At first I get all tablenames by raw 
> > query and then autoload table and put it to metadata. 
> > 
> > | 
> > # future 
> > from__future__ importwith_statement 
> > 
> > # standard library 
> > fromlogging.config importfileConfig 
> > 
> > # SQLAlchemy 
> > importsqlalchemy 
> > fromsqlalchemy importTable,MetaData,pool,engine_from_config 
> > fromsqlalchemy.ext.automap importautomap_base 
> > fromsqlalchemy.sql.elements importquoted_name 
> > fromsqlalchemy.ext.declarative importdeclarative_base 
> > 
> > # third-party 
> > fromalembic importcontext 
> > fromalembic.ddl.impl importDefaultImpl 
> > 
> > 
> > classFirebirdImpl(DefaultImpl): 
> > __dialect__ ='firebird' 
> > transactional_ddl =True 
> > 
> > 
> > # this is the Alembic Config object, which provides 
> > # access to the values within the .ini file in use. 
> > config =context.config 
> > 
> > metadata =MetaData() 
> > 
> > engine =engine_from_config( 
> > config.get_section(config.config_ini_section), 
> > prefix='sqlalchemy.', 
> > poolclass=pool.NullPool) 
> > 
> > q =engine.execute(''' 
> > select rdb$relation_name 
> > from rdb$relations 
> > where rdb$view_blr is null 
> > and (rdb$system_flag is null or rdb$system_flag = 0) 
> > ''') 
> > tables =[x[0].strip()forx inq.fetchall()] 
> > 
> > create_done =0 
> > 
> > while(notcreate_done): 
> > create_done =1 
> > fortable intables: 
> > print(table) 
> > iftable.isupper(): 
> > try: 
> > Table(table,metadata,autoload_with=engine) 
> > exceptsqlalchemy.exc.NoSuchTableErrorase: 
> > create_done =0 
> > continue 
> > try: 
> > _table =Table(quoted_name(table,True),metadata, 
> >autoload_with=engine) 
> > exceptsqlalchemy.exc.NoSuchTableErrorase: 
> > create_done =0 
> > 
> > print(metadata.tables) 
> > print(metadata.tables.keys()) 
>
>
> the stack trace indicates it's failing when it iterates through the list 
> of foreign key constraints for a table, finds one that refers to a table 
> called "readers", and then the case sensitivity isn't working out such 
> that it can't actually locate a table of that name. 
>
> if you can isolate this single pair of tables, then do a simple test 
> script that's like : 
>
>
> Table(, metadata, autoload_with=engine) 
>
> where  is the table that has a foreign key reference to 
> "readers". 
>
> then if you could show me the CREATE TABLE statements for those two 
> tables verbatim, if I can get a firebird running somewhere I can try to 
> find time to run this and look into it. 
>
>
> > 
> > # Interpret the config file for Python logging. 
> > # This line sets up loggers basically. 
> > fileConfig(config.config_file_name) 
> > 
> > # add your model's MetaData object here 
> > # for 'autogenerate' support 
> > # from myapp import mymodel 
> > # target_metadata = mymodel.Base.metadata 
> > target_metadata =metadata 
> > 
> >

Re: [sqlalchemy] How to reflect database with auto quote_name of __tablename__ ?

2016-11-13 Thread uralbash
I use quoted_name to describe the table schema (declarative method) in my 
project like this:

class People(Base):
"""
.. restapi::
:table: people
"""
__tablename__ = quoted_name('people', quote=True)

id = Column(
quoted_name("id", quote=True),
Integer, Sequence('GEN_people_ID'),
primary_key=True, autoincrement=True
)
name = Column(
quoted_name("name", quote=True),
Unicode
)

And it's work ok for me. 
Now I want to make migration for other FireBird database with auto reflect 
table, because it designed outside of python and SQLAlchemy.
I redid env.py as you suggested. At first I get all tablenames by raw query 
and then autoload table and put it to metadata.

# future
from __future__ import with_statement

# standard library
from logging.config import fileConfig

# SQLAlchemy
import sqlalchemy
from sqlalchemy import Table, MetaData, pool, engine_from_config
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.sql.elements import quoted_name
from sqlalchemy.ext.declarative import declarative_base

# third-party
from alembic import context
from alembic.ddl.impl import DefaultImpl


class FirebirdImpl(DefaultImpl):
__dialect__ = 'firebird'
transactional_ddl = True


# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

metadata = MetaData()

engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)

q = engine.execute('''
select rdb$relation_name
from rdb$relations
where rdb$view_blr is null
and (rdb$system_flag is null or rdb$system_flag = 0)
''')
tables = [x[0].strip() for x in q.fetchall()]

create_done = 0

while (not create_done):
create_done = 1
for table in tables:
print(table)
if table.isupper():
try:
Table(table, metadata, autoload_with=engine)
except sqlalchemy.exc.NoSuchTableError as e:
create_done = 0
continue
try:
_table = Table(quoted_name(table, True), metadata,
   autoload_with=engine)
except sqlalchemy.exc.NoSuchTableError as e:
create_done = 0

print(metadata.tables)
print(metadata.tables.keys())

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well.  By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""

with engine.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()


It seems now the tables are created properly but alembic still raise 
exception by table names


INFO  [alembic.runtime.migration] Context impl FirebirdImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'COMPANIES'
INFO  [alembic.autogenerate.compare] Detected added table 'CARD'
INFO  [alembic.autogenerate.compare] Detected added table 'LOCATIONS'
INFO  [alembic.autogenerate.compare] Detected added table 'RADIO'
Traceback (most recent call last):
  File "/home/uralbash/Projects/_tmp/_NotAliens/.sacrud_env/bin/alembic", 
line 9, in 
load_entry_point('alembic==0.8.8', 'console_scripts', 'alembic')()
  File 
"/home/uralbash/Projects/_tmp/_NotAliens/.sacrud_env/lib/python3.5/site-packages/alembic/config.py"
, line 479, in main
CommandLine(prog=prog).main(argv=argv)
  File 
"/home/uralbash/Projects/_tmp/_NotAliens/.sa

[sqlalchemy] How to reflect database with auto quote_name of __tablename__ ?

2016-11-11 Thread uralbash
Hello, I'm trying to reflect existing FireBird database like 
metadata.reflect(engine) it's works ok with UPPERCASE tablename but with 
lowercase name raise exception:

reflection.py, line 598, in reflecttable
raise exc.NoSuchTableError(table.name)
sqlalchemy.exc.NoSuchTableError: foo_states

I solve this problem by "__tablename__ = quoted_name('foo_states', 
quote=True)" when it created manually. Is there a possibility to specify 
quote=True 
for reflect function?

-- 
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] Firebird is no way to implement UPDATE OR INSERT

2016-06-28 Thread uralbash
Thanks, In general, API for creation of custom ClauseElements and compilers 
will solve my problem

понедельник, 27 июня 2016 г., 19:12:05 UTC+5 пользователь Mike Bayer 
написал:
>
> not currently, and we don't actually spend much time working on the 
> Firebird dialect (would love if someone could come take it over).   Your 
> best bet would be working with custom compilation rules described in 
> http://docs.sqlalchemy.org/en/latest/core/compiler.html . 
>
>
>
> On 06/27/2016 07:47 AM, uralbash wrote: 
> > 
> > Is there a way to construct a query "UPDATE OR INSERT" for firebird 
> > like Postgres "INSERT...ON CONFLICT (Upsert)" 
> > (
> http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert)?
>  
>
> > 
> > 
> > Firebird docs 
> > http://www.firebirdsql.org/refdocs/langrefupd25-update-or-insert.html 
> > 
> > -- 
> > 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+...@googlegroups.com  
> > <mailto:sqlalchemy+unsubscr...@googlegroups.com >. 
> > To post to this group, send email to sqlal...@googlegroups.com 
>  
> > <mailto:sqlal...@googlegroups.com >. 
> > Visit this group at https://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 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] Firebird is no way to implement UPDATE OR INSERT

2016-06-27 Thread uralbash
Is there a way to construct a query "UPDATE OR INSERT" for firebird like 
Postgres "INSERT...ON CONFLICT (Upsert)" 
(http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert)?


Firebird docs 
http://www.firebirdsql.org/refdocs/langrefupd25-update-or-insert.html

-- 
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] How to add custom attribute to the session?

2015-03-30 Thread uralbash
More example in tests 
https://github.com/ITCase/sacrud/blob/master/sacrud/tests/test_sessionmaker.py

def test_create(self):
user = self.session.sacrud(User)\
.create({'name': 'Dzhirad', 'fullname': 'Kiri', 'password': 123})
self.assertEqual(user.name, 'Dzhirad')
db_user = self.session.query(User).get(user.id)
self.assertEqual(user.id, db_user.id)

def test_read(self):
self._add_item(User, 'Vasya', 'Pupkin', 123)
user = self.session.sacrud(User).read(1)
self.assertEqual(user.name, 'Vasya')
self.assertEqual(user.id, 1)

def test_update(self):
self._add_item(User, 'Vasya', 'Pupkin', 123)
user = self.session.sacrud(User).update(1, {'name': 'Bill'})
db_user = self.session.query(User).get(user.id)
self.assertEqual(user.name, 'Bill')
self.assertEqual(user.id, db_user.id)

def test_delete(self):
user = self._add_item(User, 'Volod', 'Khonin', 123)
self.session.sacrud(User).delete(user.id)
db_user = self.session.query(User).filter_by(id=user.id).all()
self.assertEqual(db_user, [])




понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King 
написал:

 On Mon, Mar 30, 2015 at 1:09 PM, uralbash svint...@gmail.com 
 javascript: wrote: 
  Hello, 
  
  I want to set up in each session was my custom attribute, for example: 
  
  from sqlalchemy.orm import scoped_session, sessionmaker 
  
  Session = my_wapper(scoped_session(sessionmaker())) 
  session1 = Session() 
  session2 = Session() 
  
  hasattr(session1, foo)  # True: What I want 
  hasattr(session2, foo)  # True: What I want 
  print(session1.foo) 
  
  What is the best way to do it? I have no idea how I can change the 
 object 
  from __call__ function. I also did not find that something like 
  Session.set_property. 
  

 You can define a subclass of the sqlalchemy Session, and ask 
 sessionmaker to return instances of that class by passing the class_ 
 parameter to sessionmaker(): 


 http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker
  

 Would that do what you wanted? 

 Simon 


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


Re: [sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread uralbash
I'm writing a module (sacrud https://github.com/ITCase/sacrud) that is not 
initially aware of the session. I want to add my session method (sacrud).

from sqlalchemy.orm import scoped_session, sessionmaker
from sacrud import crud_sessionmaker

DBSession = crud_sessionmaker(scoped_session(sessionmaker()))
DBSession.sacrud.mycustommethod


See example there 
https://github.com/ITCase/sacrud/blob/master/sacrud/__init__.py

понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King 
написал:

 On Mon, Mar 30, 2015 at 1:09 PM, uralbash svint...@gmail.com 
 javascript: wrote: 
  Hello, 
  
  I want to set up in each session was my custom attribute, for example: 
  
  from sqlalchemy.orm import scoped_session, sessionmaker 
  
  Session = my_wapper(scoped_session(sessionmaker())) 
  session1 = Session() 
  session2 = Session() 
  
  hasattr(session1, foo)  # True: What I want 
  hasattr(session2, foo)  # True: What I want 
  print(session1.foo) 
  
  What is the best way to do it? I have no idea how I can change the 
 object 
  from __call__ function. I also did not find that something like 
  Session.set_property. 
  

 You can define a subclass of the sqlalchemy Session, and ask 
 sessionmaker to return instances of that class by passing the class_ 
 parameter to sessionmaker(): 


 http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker
  

 Would that do what you wanted? 

 Simon 


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


[sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread uralbash
Hello,

I want to set up in each session was my custom attribute, for example:

from sqlalchemy.orm import scoped_session, sessionmaker

Session = my_wapper(scoped_session(sessionmaker()))
session1 = Session()
session2 = Session()

hasattr(session1, foo)  # True: What I want
hasattr(session2, foo)  # True: What I want
print(session1.foo)

What is the best way to do it? I have no idea how I can change the object 
from __call__ function. I also did not find that something like 
Session.set_property.

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


Re: [sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread uralbash
Yes, it works. Thank you so much.

понедельник, 30 марта 2015 г., 19:58:40 UTC+5 пользователь Simon King 
написал:

 Can't you write it like this (untested)? 

 # 
 class CRUDSession(sqlalchemy.orm.Session): 
 def sacrud(self, cls): 
 return CRUD(self, cls) 

 Session = scoped_session(sessionmaker(class_=CRUDSession)) 
 # 

 Simon 

 On Mon, Mar 30, 2015 at 3:15 PM, uralbash svint...@gmail.com 
 javascript: wrote: 
  More example in tests 
  
 https://github.com/ITCase/sacrud/blob/master/sacrud/tests/test_sessionmaker.py
  
  
  def test_create(self): 
  user = self.session.sacrud(User)\ 
  .create({'name': 'Dzhirad', 'fullname': 'Kiri', 'password': 
  123}) 
  self.assertEqual(user.name, 'Dzhirad') 
  db_user = self.session.query(User).get(user.id) 
  self.assertEqual(user.id, db_user.id) 
  
  def test_read(self): 
  self._add_item(User, 'Vasya', 'Pupkin', 123) 
  user = self.session.sacrud(User).read(1) 
  self.assertEqual(user.name, 'Vasya') 
  self.assertEqual(user.id, 1) 
  
  def test_update(self): 
  self._add_item(User, 'Vasya', 'Pupkin', 123) 
  user = self.session.sacrud(User).update(1, {'name': 'Bill'}) 
  db_user = self.session.query(User).get(user.id) 
  self.assertEqual(user.name, 'Bill') 
  self.assertEqual(user.id, db_user.id) 
  
  def test_delete(self): 
  user = self._add_item(User, 'Volod', 'Khonin', 123) 
  self.session.sacrud(User).delete(user.id) 
  db_user = self.session.query(User).filter_by(id=user.id).all() 
  self.assertEqual(db_user, []) 
  
  
  
  
  понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King 
  написал: 
  
  On Mon, Mar 30, 2015 at 1:09 PM, uralbash svint...@gmail.com wrote: 
   Hello, 
   
   I want to set up in each session was my custom attribute, for 
 example: 
   
   from sqlalchemy.orm import scoped_session, sessionmaker 
   
   Session = my_wapper(scoped_session(sessionmaker())) 
   session1 = Session() 
   session2 = Session() 
   
   hasattr(session1, foo)  # True: What I want 
   hasattr(session2, foo)  # True: What I want 
   print(session1.foo) 
   
   What is the best way to do it? I have no idea how I can change the 
   object 
   from __call__ function. I also did not find that something like 
   Session.set_property. 
   
  
  You can define a subclass of the sqlalchemy Session, and ask 
  sessionmaker to return instances of that class by passing the class_ 
  parameter to sessionmaker(): 
  
  
  
 http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker
  
  
  Would that do what you wanted? 
  
  Simon 
  
  -- 
  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+...@googlegroups.com javascript:. 
  To post to this group, send email to sqlal...@googlegroups.com 
 javascript:. 
  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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] SQLAlchemy before_update called instead after_delete when I delete instance

2014-04-23 Thread uralbash
Thanks Michael,

It turns out that when remove node with children, sqlalchemy by defaults 
first update children (set parent_id=None) and then delete node. When I set 
cascade=all,delete it's delete node one by one (w/o update).

вторник, 22 апреля 2014 г., 20:39:21 UTC+6 пользователь Michael Bayer 
написал:

 not clear.Sometimes, the unit of work does different things based on 
 the random ordering of dictionaries, and arbitrary changes in code can 
 trigger these ordering changes.  you might want to try running the script 
 repeatedly to make sure these two behaviors are consistent; then try to 
 slowly remove one by one things from that extra event listen; eg. have it 
 listen on a different function, then remove elements from that function one 
 by one until you find what triggers the difference.




 On Apr 22, 2014, at 4:38 AM, uralbash svint...@gmail.com javascript: 
 wrote:

 Can anybody tell what I'm doing wrong

 I have method with which register event (before_update, after_delete, 
 before_insert) 
 https://github.com/ITCase/sqlalchemy_mptt/blob/b0efead7a8ee5acc063b68ee3bfc756af4689d6e/sqlalchemy_mptt/mixins.py#L69

 @classmethoddef register_tree(cls):
 event.listen(cls, before_insert, cls.mptt_before_insert)
 event.listen(cls, after_delete, cls.mptt_after_delete)
 event.listen(cls, before_update, cls.mptt_before_update)


 When I registered event and try to delete row (
 https://github.com/ITCase/sqlalchemy_mptt/blob/b0efead7a8ee5acc063b68ee3bfc756af4689d6e/sqlalchemy_mptt/tests.py#L104
 ).

 class Tree(Base, BaseNestedSets):
 __tablename__ = tree

 id = Column(Integer, primary_key=True)
 Tree.register_tree()
 def test_delete_node(self):
 node = self.session.query(Tree).filter(Tree.id == 4).one()
 self.session.delete(node)
 # id lft rgt lvl parent tree
 self.assertEqual([(1, 1, 16, 1, None, 1),
   (2, 2, 5, 2, 1, 1),
   (3, 3, 4, 3, 2, 1),
   (7, 6, 15, 2, 1, 1),
   (8, 7, 10, 3, 7, 1),
   (9, 8, 9, 4, 8, 1),
   (10, 11, 14, 3, 7, 1),
   (11, 12, 13, 4, 10, 1)], self.result.all())

 Called before_update method instead after_delete. But if I comment 
 before_update event (
 https://github.com/ITCase/sqlalchemy_mptt/blob/b0efead7a8ee5acc063b68ee3bfc756af4689d6e/sqlalchemy_mptt/mixins.py#L72)
  
 it's work fine.

 @classmethoddef register_tree(cls):
 event.listen(cls, before_insert, cls.mptt_before_insert)
 event.listen(cls, after_delete, cls.mptt_after_delete)
 # event.listen(cls, before_update, cls.mptt_before_update) -- IF 
 comment this, called after_delete method. It's OK.


 Build status https://travis-ci.org/ITCase/sqlalchemy_mptt/builds/23428309

 What's wrong?

 I asked this on 
 stackoverflow.comhttp://stackoverflow.com/questions/23198252/sqlalchemy-before-update-called-instead-after-delete

 -- 
 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+...@googlegroups.com javascript:.
 To post to this group, send email to sqlal...@googlegroups.comjavascript:
 .
 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] SQLAlchemy before_update called instead after_delete when I delete instance

2014-04-22 Thread uralbash
 

Can anybody tell what I'm doing wrong

I have method with which register event (before_update, after_delete, 
before_insert) 
https://github.com/ITCase/sqlalchemy_mptt/blob/b0efead7a8ee5acc063b68ee3bfc756af4689d6e/sqlalchemy_mptt/mixins.py#L69

@classmethoddef register_tree(cls):
event.listen(cls, before_insert, cls.mptt_before_insert)
event.listen(cls, after_delete, cls.mptt_after_delete)
event.listen(cls, before_update, cls.mptt_before_update)


When I registered event and try to delete row (
https://github.com/ITCase/sqlalchemy_mptt/blob/b0efead7a8ee5acc063b68ee3bfc756af4689d6e/sqlalchemy_mptt/tests.py#L104
).

class Tree(Base, BaseNestedSets):
__tablename__ = tree

id = Column(Integer, primary_key=True)
Tree.register_tree()
def test_delete_node(self):
node = self.session.query(Tree).filter(Tree.id == 4).one()
self.session.delete(node)
# id lft rgt lvl parent tree
self.assertEqual([(1, 1, 16, 1, None, 1),
  (2, 2, 5, 2, 1, 1),
  (3, 3, 4, 3, 2, 1),
  (7, 6, 15, 2, 1, 1),
  (8, 7, 10, 3, 7, 1),
  (9, 8, 9, 4, 8, 1),
  (10, 11, 14, 3, 7, 1),
  (11, 12, 13, 4, 10, 1)], self.result.all())

Called before_update method instead after_delete. But if I comment 
before_update event (
https://github.com/ITCase/sqlalchemy_mptt/blob/b0efead7a8ee5acc063b68ee3bfc756af4689d6e/sqlalchemy_mptt/mixins.py#L72)
 
it's work fine.

@classmethoddef register_tree(cls):
event.listen(cls, before_insert, cls.mptt_before_insert)
event.listen(cls, after_delete, cls.mptt_after_delete)
# event.listen(cls, before_update, cls.mptt_before_update) -- IF comment 
this, called after_delete method. It's OK.


Build status https://travis-ci.org/ITCase/sqlalchemy_mptt/builds/23428309

What's wrong?

I asked this on 
stackoverflow.comhttp://stackoverflow.com/questions/23198252/sqlalchemy-before-update-called-instead-after-delete

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