[sqlalchemy] Re: Sql alchemy-Oracle Error

2010-05-19 Thread dhanil anupurath
Hi

thanks for the quick reply and pointing me in the right direction.
it seems the error is actually related to the   operator.
is there anything SA can do on this issue?
In the meantime i am going to change my query.

thanks again..

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] ConcurrentModificationError: Updated rowcount 0 does not match number of objects updated 1

2010-05-19 Thread jose soares

Hi all,

Someone knows what this error mean?
...

 File 
/home/ve/sfera/release/sicer/BASE/model/anagraficaAlta/unita_aziendale.py, 
line 154, in aggiorna_capi_bovini
   x.flush()
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/ext/assignmapper.py,
 line 20, in do
   return getattr(session, name)(self, *args, **kwargs)
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/orm/session.py,
 line 319, in flush
   self.uow.flush(self, objects)
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/orm/unitofwork.py,
 line 210, in flush
   flush_context.execute()
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/orm/unitofwork.py,
 line 400, in execute
   UOWExecutor().execute(self, head)
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/orm/unitofwork.py,
 line 1020, in execute
   self.execute_delete_steps(trans, task)
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/orm/unitofwork.py,
 line 1043, in execute_delete_steps
   self.delete_objects(trans, task)
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/orm/unitofwork.py,
 line 1026, in delete_objects
   task.mapper.delete_obj(task.polymorphic_todelete_objects, trans)
 File 
/home/ve/lib/python2.5/site-packages/SQLAlchemy-0.3.10-py2.5.egg/sqlalchemy/orm/mapper.py,
 line 1322, in delete_obj
   raise exceptions.ConcurrentModificationError(Updated rowcount %d does not match 
number of objects updated %d % (c.rowcount, len(delete)))

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Order of INSERTs for children of an object

2010-05-19 Thread Tomasz Nazar
On Sat, Jan 17, 2009 at 3:40 AM, Michael Bayer mike...@zzzcomputing.com wrote:
 On Jan 16, 2009, at 6:13 PM, Tomasz Nazar wrote:


 Do I have control over this behaviour? Or am I doing sth wrong?
 Thanks for any help..Tomasz


 you can !   its an exposed internal API but its pretty stable for
 now.  Build a SessionExtension like this:

 from sqlalchemy.orm.interfaces import SessionExtension

 class SetupDependencyExt(SessionExtension):
     def before_flush(self, session, flush_context, instances):
         flush_context.register_dependency(class_mapper(A),
 class_mapper(B))

 where A is the mapper that should execute first.   then configure
 that extension with your sessionmaker(extension=SetupDependencyExt()).

Hi Mike,
I'm migrating from 0.4 to 0.6 and this API has been removed - at least
looks like this. Is there any replacement?
I'd still like SQLA to generate some INSERT statements before others,
as it looks SQLA0.4 could'nt handle mappers/relations/dependencies I
have...
This code worked well for me till now...

Code I have is:

sessionmaker(...extension=SetupDependencyExt())

class SetupDependencyExt(SessionExtension):

http://groups.google.com/group/sqlalchemy/browse_thread/thread/2420cbf64ff5a74f
For controlling order of inserts generation for dependent children
by relation

def before_flush(self, session, flush_context, instances):
flush_context.register_dependency(mapper_conference_lang,
mapper_conference_participant)
flush_context.register_dependency(mapper_conference_lang,
mapper_conference_interpreter)

AttributeError: 'UOWTransaction' object has no attribute 'register_dependency'

I'd really appreciate any help, as I can't move forward without it.
Thanks - Tomasz


-- 
_i__'simplicity_is_the_key'__tomasz_nazar
_ii'i_am_concern_oriented'JKM-UPR
_iii__'patsystem.sf.net'___linux_user
_'aspectized.com'___prevayler

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] CheckConstraint compatibility

2010-05-19 Thread jose soares

Hi all,

I have to create a constraint like this:

   CheckConstraint('data_start = CURRENT_DATE'),

it works for PostgreSQL but it doesn't work for Oracle10.

Is there some workaround to make it compatible with pg and oracle?

j

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] default value in multiple insert

2010-05-19 Thread Michael Bayer

On May 18, 2010, at 10:06 PM, Dan Kuebrich wrote:

 My apologies; I expressed my question rather incoherently.
 
 if the question is, I want to multiple insert like [{'a':1, 'b':2, 'c':3}, 
 {'a':2}, {'a':3, 'b':4}], etc. only some dictionaries are missing different 
 keys, that is not allowed.   The structure of the SQL statement as parsed by 
 MySQL determines for which columns the server-side default is emitted and 
 there is only one version of that with an executemany.   So every param 
 dictionary must have at least all the keys which the first one does.
 
 This was the basic scenario of my question. However, unless I misunderstand, 
 it does seem to be allowed: the missing values are replaced with NULL in 
 the generated query. 

it should not be in SQLA 0.6.Specific logic was added to disallow this.


 But what I actually meant to ask was this: sqlalchemy handles missing 
 dictionary entries as NULL for the purpose of inserts, is there an easy way 
 to have it generate a SQL statement with DEFAULT for missing dictionary 
 entries?
 
 Eg. execute(testtable.insert(), [{'a':1, 'b':2, 'c':3}, {'a':2}, {'a':3, 
 'b':4}]) = INSERT INTO testtable (a,b,c) VALUES 
 (1,2,3),(2,DEFAULT,DEFAULT),(3,4,DEFAULT)

ive never seen that syntax before (i.e. DEFAULT is actually present as a 
value).Assuming its valid, you could achieve it using a client side 
default as I mentioned earlier.i.e. Column('foo', Integer, 
default=text('DEFAULT')).  But again, not with an executemany() and 
heterogeneous dictinoaries as you have above. The SQL statement is rendered 
only once, and either has a bind parameter for a particular position or not.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Sql alchemy-Oracle Error

2010-05-19 Thread Michael Bayer
I'm not familiar with the  operator in Oracle or its syntax.  The first step 
would be to create a cx_oracle sample script that emits the exact SQL you're 
looking for.   We could then see how to adapt it to SQLAlchemy.


On May 19, 2010, at 2:45 AM, dhanil anupurath wrote:

 Hi
 
 thanks for the quick reply and pointing me in the right direction.
 it seems the error is actually related to the   operator.
 is there anything SA can do on this issue?
 In the meantime i am going to change my query.
 
 thanks again..
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Default values not correctly parsed from DB

2010-05-19 Thread Andrew
We're using ORM to do unit testing, so we're mocking up the commit
message to do nothing, basically creating a long transaction that's
rolled back at the end of the test.  However, I am running into the
following problem.  Assume we've mapped a table to class MyTable with
a varchar `name' and a boolean `flag' that defaults to false:

Session.add(MyTable(name=Bob))
bob = Session.query(MyTable).filter_by(name=Bob).first()
bob.flag = True

Session.query(MyTable).filter_by(name=Bob).first()
Session.delete(bob)

# At this point, Bob does not exist and doing a query *will* fail
within the transaction
Session.add(MyTable(name=Bob))
bob = Session.query(MyTable).filter_by(name=Bob).first()
assert_equals(bob.flag, False)

This now fails with bob.flag still being set to True from the previous
update, even though the default value in the DB is set to be False by
default (and works correctly on the initial insert).  Running this
with commits turned on does not run into this problem.

Before submitting a bug, I want to make sure there's not a config
setting somewhere that we're missing.  We are running this against
postgresql 8.4 with sqlalchemy 0.6.

Thank you!

Andrew

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] underscore in mssql column names?

2010-05-19 Thread mte
Hi,
I need to connect to an existing MS SQL database with SqlAlchemy. I'm
using a declarative_base (haven't tried with manual mapping) and it
seems to work except for a few fields.
Those are all defined as col1 =
Column('some_field_with_underscores_in_name', AnyType)
(AnyType meaning Integer, String or whatever else).
If I do a session.query(MyClass).first().col1 I get None returned. I
get the correct value if I do session.query(MyClass).first().col2
(which doesn't have underscores in its name) or if I fetch the row
with session.execute(SELECT * FROM mytable).
Can this be considered a bug or is there a setting I haven't specified
correctly?

Thanks,
Matej

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] google groups duplicating emails

2010-05-19 Thread Michael Bayer
If you're experiencing duplicated emails received from the group, others seem 
to be reporting on this too:

http://groups.google.com/group/is-something-broken/browse_thread/thread/58bd16a30c48d35e#
http://groups.google.com/group/is-something-broken/browse_thread/thread/9ddcd435f206a3ce#

I'm sure the Google engineers respond first to the threads that have lots of 
activity, hint hint.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] underscore in mssql column names?

2010-05-19 Thread Michael Bayer
there's no issue with underscore column names so something else must be going 
on.

On May 19, 2010, at 7:47 AM, mte wrote:

 Hi,
 I need to connect to an existing MS SQL database with SqlAlchemy. I'm
 using a declarative_base (haven't tried with manual mapping) and it
 seems to work except for a few fields.
 Those are all defined as col1 =
 Column('some_field_with_underscores_in_name', AnyType)
 (AnyType meaning Integer, String or whatever else).
 If I do a session.query(MyClass).first().col1 I get None returned. I
 get the correct value if I do session.query(MyClass).first().col2
 (which doesn't have underscores in its name) or if I fetch the row
 with session.execute(SELECT * FROM mytable).
 Can this be considered a bug or is there a setting I haven't specified
 correctly?
 
 Thanks,
 Matej
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] underscore in mssql column names?

2010-05-19 Thread Conor




On 05/19/2010 06:47 AM, mte wrote:

  Hi,
I need to connect to an existing MS SQL database with SqlAlchemy. I'm
using a declarative_base (haven't tried with manual mapping) and it
seems to work except for a few fields.
Those are all defined as "col1 =
Column('some_field_with_underscores_in_name', AnyType)"
(AnyType meaning Integer, String or whatever else).
If I do a session.query(MyClass).first().col1 I get None returned. I
get the correct value if I do session.query(MyClass).first().col2
(which doesn't have underscores in its name) or if I fetch the row
with session.execute("SELECT * FROM mytable").
Can this be considered a bug or is there a setting I haven't specified
correctly?

  

Here is a possible reason: SQLAlchemy has a "use_labels" feature
which adds table/alias prefixes to columns in the columns clause to
help disambiguate columns from different tables with the same name. It
could be that your col1 is named in such a way that makes
SQLAlchemy think that "use_labels" is in effect, which could cause it
to not recognize it properly. For example, if "MyClass" uses the "myclass"
table, and col1's name is "myclass_id", then SQLAlchemy may try to
interpret that as the "id" column of "myclass" instead of the
"myclass_id" column.

If this is indeed the case, the problem should go away if you really
enable the "use_labels" feature:

session.query(MyClass).with_labels().first().col1

If this does not fix it, please post the SQL that is emitted by
SQLAlchemy when you use session.query(MyClass).first(). You
can see the emitted SQL by passing echo=True to create_engine.
-Conor






-- 
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.




[sqlalchemy] Re: Default values not correctly parsed from DB

2010-05-19 Thread Andrew
Ah, I understand the reasoning now.  Adding the flush in between the
delete and add did just the trick; thank you for the explanation,
Mike!

Andrew

On May 19, 10:29 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On May 19, 2010, at 10:16 AM, Andrew wrote:



  We're using ORM to do unit testing, so we're mocking up the commit
  message to do nothing, basically creating a long transaction that's
  rolled back at the end of the test.  However, I am running into the
  following problem.  Assume we've mapped a table to class MyTable with
  a varchar `name' and a boolean `flag' that defaults to false:

     Session.add(MyTable(name=Bob))
     bob = Session.query(MyTable).filter_by(name=Bob).first()
     bob.flag = True

     Session.query(MyTable).filter_by(name=Bob).first()
     Session.delete(bob)

     # At this point, Bob does not exist and doing a query *will* fail
  within the transaction
     Session.add(MyTable(name=Bob))
     bob = Session.query(MyTable).filter_by(name=Bob).first()
     assert_equals(bob.flag, False)

  This now fails with bob.flag still being set to True from the previous
  update, even though the default value in the DB is set to be False by
  default (and works correctly on the initial insert).  Running this
  with commits turned on does not run into this problem.

  Before submitting a bug, I want to make sure there's not a config
  setting somewhere that we're missing.  We are running this against
  postgresql 8.4 with sqlalchemy 0.6.

 this is the expected behavior as a flush() that receives a delete() and 
 insert() of the same effective row (assuming name is the primary key here) 
 is converted to an UPDATE.   If you put a flush() after the delete() of bob1 
 the next add() would result in an insert.

 this behavior is due to the fact that the unit of work emits all 
 inserts/updates before all the deletes within a single flush.   the UOW 
 rewrite of 0.6 broke the very first ground on us being able to make this 
 behavior more flexible, but such functionality is a long way off.

 --
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group 
 athttp://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Override onupdate timestamp

2010-05-19 Thread Bryan
I have a table 'table', with a column, 'stamp', that has an onupdate
clause onupdate=datetime.now.

I am trying to update table.otherColumn, and I don't want table.stamp
to be updated with the latest time.

I saw one discussion about overriding onupdate here:
http://groups.google.com/group/sqlalchemy/browse_thread/thread/815734c0a383c6ac/be8fc1c1535a70ff?lnk=gstq=onupdate+override#be8fc1c1535a70ff

...but in that case, we knew what we wanted the override value to be
for the column, which was setting it to True.  At the time of building
my update, I'm not sure what table.stamp is equal to, so I don't know
what I should override it with.

Is it possible to avoid the onupdate clause from executing?



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: Is multi-level polymorphism possible in SQLAlchemy?

2010-05-19 Thread Kiran Jonnalagadda
Thanks. I've got this working now, but am having trouble combining
polymorphism and multiple foreign keys to the same table. Here's
roughly what I'm doing:

class Entity(Base):
__tablename__ = 'entities'
id = Column(Integer, primary_key=True)
# ...bunch of columns...
type = Column(Unicode(20), nullable=False)
__mapper_args__ = {'polymorphic_on': type}

class Site(Entity): # Client account
__tablename__ = 'sites'
__mapper_args__ = {'polymorphic_identity': u'site'}
id = Column(Integer, ForeignKey('entities.id'), primary_key=True)
name = Column(Unicode(50), unique=True, nullable=False)
title = Column(Unicode(50), nullable=False)

# ...few more Entity derived models...

class Note(Entity):
__tablename__ = 'notes'
__mapper_args__ = {'polymorphic_identity': u'note',
   'inherit_condition': (id == Entity.id)}
id = Column(Integer, ForeignKey('entities.id'), primary_key=True)
site_id = Column(Integer, ForeignKey('deals.id'), nullable=False)
site = relation(Site, foreign_keys=site_id,
primaryjoin=site_id == Site.id)
attached_to_id = Column(Integer, ForeignKey('entities.id'),
nullable=False)
attached_to = relation(Entity, foreign_keys=attached_to_id,
   primaryjoin=attached_to_id == Entity.id,
backref=backref('notes', cascade='all, delete-
orphan'))
note = Column(Unicode(255), nullable=False)

I use Note.site to determine access rights and Note.attached_to to
determine containment. This declaration works until I try to delete a
site instance. SQLAlchemy throws up this exception:

...
File /usr/local/lib/python2.6/dist-packages/sqlalchemy/sql/
compiler.py, line 287, in construct_params
pd[self.bind_names[bindparam]] = bindparam.value()
TypeError: id() takes exactly one argument (0 given)

However, if I remove the backref on attached_to, everything works
fine. If I move the backref to Note.site, it fails with the same
error. If I remove attached_to* and keep the backref on site, it works
again. I'm not sure what's wrong.

Here's the full traceback, for reference.

Traceback (most recent call last):
  File /home/jace/Projects/saproj/tests.py, line 76, in test_cascade
self.session.delete(site)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
session.py, line 1088, in delete
cascade_states = list(_cascade_state_iterator('delete', state))
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
session.py, line 1534, in _cascade_state_iterator
for (o, m) in mapper.cascade_iterator(cascade, state, **kwargs):
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
mapper.py, line 1229, in cascade_iterator
instance, instance_mapper, corresponding_state  = iterator.next()
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
properties.py, line 703, in cascade_iterator
instances = state.value_as_iterable(self.key, passive=passive)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
state.py, line 128, in value_as_iterable
x = impl.get(self, dict_, passive=passive)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
attributes.py, line 377, in get
value = callable_(passive=passive)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
strategies.py, line 563, in __call__
result = q.all()
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
query.py, line 1286, in all
return list(self)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
query.py, line 1394, in __iter__
return self._execute_and_instances(context)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
query.py, line 1399, in _execute_and_instances
mapper=self._mapper_zero_or_none())
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
session.py, line 737, in execute
clause, params or {})
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/
base.py, line 1086, in execute
return Connection.executors[c](self, object, multiparams, params)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/
base.py, line 1149, in _execute_clauseelement
parameters=params
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/
base.py, line 1237, in __create_execution_context
return dialect.execution_ctx_cls(dialect, connection=self,
**kwargs)
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/
default.py, line 355, in __init__
grp,m in enumerate(parameters)]
  File /usr/local/lib/python2.6/dist-packages/sqlalchemy/sql/
compiler.py, line 287, in construct_params
pd[self.bind_names[bindparam]] = bindparam.value()
TypeError: id() takes exactly one argument (0 given)

Thanks for the help.


On May 18, 10:42 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Don't use None for the Column type (i.e., detected as the null type).  Put 
 the type explicitly.   This has been updated in the documentation recently 
 since the None 

Re: [sqlalchemy] default value in multiple insert

2010-05-19 Thread Dan Kuebrich
Michael--thanks for your help. As you probably guessed, I'm still running
0.5. DEFAULT is, in fact, a valid MySQL keyword, though I didn't know about
it before looking into this problem.

On Wed, May 19, 2010 at 10:09 AM, Michael Bayer mike...@zzzcomputing.comwrote:


 On May 18, 2010, at 10:06 PM, Dan Kuebrich wrote:

 My apologies; I expressed my question rather incoherently.

  if the question is, I want to multiple insert like [{'a':1, 'b':2, 'c':3},
 {'a':2}, {'a':3, 'b':4}], etc. only some dictionaries are missing different
 keys, that is not allowed.   The structure of the SQL statement as parsed by
 MySQL determines for which columns the server-side default is emitted and
 there is only one version of that with an executemany.   So every param
 dictionary must have at least all the keys which the first one does.


 This was the basic scenario of my question. However, unless I
 misunderstand, it does seem to be allowed: the missing values are replaced
 with NULL in the generated query.


 it should not be in SQLA 0.6.Specific logic was added to disallow this.


 But what I actually meant to ask was this: sqlalchemy handles missing
 dictionary entries as NULL for the purpose of inserts, is there an easy way
 to have it generate a SQL statement with DEFAULT for missing dictionary
 entries?

 Eg. execute(testtable.insert(), [{'a':1, 'b':2, 'c':3}, {'a':2}, {'a':3,
 'b':4}]) = INSERT INTO testtable (a,b,c) VALUES
 (1,2,3),(2,DEFAULT,DEFAULT),(3,4,DEFAULT)


 ive never seen that syntax before (i.e. DEFAULT is actually present as a
 value).Assuming its valid, you could achieve it using a client side
 default as I mentioned earlier.i.e. Column('foo', Integer,
 default=text('DEFAULT')).  But again, not with an executemany() and
 heterogeneous dictinoaries as you have above. The SQL statement is
 rendered only once, and either has a bind parameter for a particular
 position or not.



  --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to
 sqlalchemy+unsubscr...@googlegroups.comsqlalchemy%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] post_update fails with backrefs set

2010-05-19 Thread Brad Wells
In upgrading to 0.6.0 I've found a regression in behavior for
relationships with backrefs that require post_update=True.

from sqlalchemy import Table, Column, Integer, String, MetaData,
create_engine, ForeignKey, ForeignKeyConstraint
from sqlalchemy.orm import relation, sessionmaker, scoped_session,
synonym, object_session, mapper, backref

engine = create_engine('sqlite:///', echo=False)

Session = scoped_session(sessionmaker(autoflush=True,
autocommit=False, bind=engine))
metadata = MetaData()

contacts = Table('contacts', metadata,
  Column('id', Integer, primary_key=True),
  Column('name', String(35)),
  Column('account_id', Integer),
  ForeignKeyConstraint(['account_id'], ['accounts.id']))

accounts = Table('accounts', metadata,
 Column('id', Integer, primary_key=True),
 Column('name', String(35)),
 Column('created_by_id', Integer),
 ForeignKeyConstraint(['created_by_id'],
['contacts.id'], name='fk1', use_alter=True))

class Contact(object): pass
class Account(object): pass

mapper(Contact, contacts, properties={
'account': relation(Account,
 
primaryjoin=contacts.c.account_id==accounts.c.id,
post_update=True,
#backref=backref('contacts', post_update=True)
backref='contacts'
)
})
mapper(Account, accounts, properties={
'created_by': relation(Contact,
primaryjoin=accounts.c.created_by_id==contacts.c.id),
#'contacts': relation(Contact,
primaryjoin=accounts.c.id==contacts.c.account_id)
})

metadata.create_all(bind=engine)

# create basic data
frank = Contact()
frank.name = Frank
Session.add(frank)

finc = Account()
finc.name = Frank Inc.
frank.account = finc
Session.add(finc)

Session.commit()
Session.expunge_all()

# reproduce the problem
bob = Contact()
bob.name = 'Bob'
Session.add(bob)

frank = Session.query(Contact).filter_by(name='Frank').first()
finc = frank.account

bob.account = finc

assert bob.account == finc
Session.commit()
assert bob.account == finc

If either backref on the 'account' relation of Contact is set then the
UPDATE fails to issue. If the 'contacts' relation is defined directly
on Account or the backref isn't defined at all then everything works
as expected. This is tested on SQLAlchemy 0.6.0 with Python 2.6. I
verified this as working under 0.5.8.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] post_update fails with backrefs set

2010-05-19 Thread Michael Bayer
that's very likely to be a bug related to the new unit of work.   I've added 
#1807 which is on a high priority 0.6.1 track, will try to get it in before 
0.6.1 release.


On May 19, 2010, at 3:34 PM, Brad Wells wrote:

 In upgrading to 0.6.0 I've found a regression in behavior for
 relationships with backrefs that require post_update=True.
 
 from sqlalchemy import Table, Column, Integer, String, MetaData,
 create_engine, ForeignKey, ForeignKeyConstraint
 from sqlalchemy.orm import relation, sessionmaker, scoped_session,
 synonym, object_session, mapper, backref
 
 engine = create_engine('sqlite:///', echo=False)
 
 Session = scoped_session(sessionmaker(autoflush=True,
 autocommit=False, bind=engine))
 metadata = MetaData()
 
 contacts = Table('contacts', metadata,
  Column('id', Integer, primary_key=True),
  Column('name', String(35)),
  Column('account_id', Integer),
  ForeignKeyConstraint(['account_id'], ['accounts.id']))
 
 accounts = Table('accounts', metadata,
 Column('id', Integer, primary_key=True),
 Column('name', String(35)),
 Column('created_by_id', Integer),
 ForeignKeyConstraint(['created_by_id'],
 ['contacts.id'], name='fk1', use_alter=True))
 
 class Contact(object): pass
 class Account(object): pass
 
 mapper(Contact, contacts, properties={
'account': relation(Account,
 
 primaryjoin=contacts.c.account_id==accounts.c.id,
post_update=True,
#backref=backref('contacts', post_update=True)
backref='contacts'
)
 })
 mapper(Account, accounts, properties={
'created_by': relation(Contact,
 primaryjoin=accounts.c.created_by_id==contacts.c.id),
#'contacts': relation(Contact,
 primaryjoin=accounts.c.id==contacts.c.account_id)
 })
 
 metadata.create_all(bind=engine)
 
 # create basic data
 frank = Contact()
 frank.name = Frank
 Session.add(frank)
 
 finc = Account()
 finc.name = Frank Inc.
 frank.account = finc
 Session.add(finc)
 
 Session.commit()
 Session.expunge_all()
 
 # reproduce the problem
 bob = Contact()
 bob.name = 'Bob'
 Session.add(bob)
 
 frank = Session.query(Contact).filter_by(name='Frank').first()
 finc = frank.account
 
 bob.account = finc
 
 assert bob.account == finc
 Session.commit()
 assert bob.account == finc
 
 If either backref on the 'account' relation of Contact is set then the
 UPDATE fails to issue. If the 'contacts' relation is defined directly
 on Account or the backref isn't defined at all then everything works
 as expected. This is tested on SQLAlchemy 0.6.0 with Python 2.6. I
 verified this as working under 0.5.8.
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] CheckConstraint compatibility

2010-05-19 Thread jo

Michael Bayer wrote:

On May 19, 2010, at 5:34 AM, jose soares wrote:

  

Hi all,

I have to create a constraint like this:

  CheckConstraint('data_start = CURRENT_DATE'),

it works for PostgreSQL but it doesn't work for Oracle10.

Is there some workaround to make it compatible with pg and oracle?



Assuming there's a way to create such a CHECK constraint on Oracle, you should 
use the DDL() construct with per-dialect instructions for Oracle - and for your 
CheckConstraint that works on PG, set it up with an AddConstraint that is 
specific to Postgresql.

http://www.sqlalchemy.org/docs/metadata.html#customizing-ddl
  
Oracle doesn't support such constrtaint, then I have to find a way to 
create it, not at db level, but in the code.
My hope was to be able to define it, only at SQLAlchemy level, like we 
do with some sa functions.


j

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.