[sqlalchemy] Re: History meta and unique issue

2011-10-18 Thread Damian
Hi Mike,

Brilliant thanks - that works - spent ages staring at it and missed
the second col.copy()!

Damian

On Oct 17, 6:27 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 ah the example has a bug, you're hitting a second part of the code there 
 where it fetches columns that are placed on a single table child class that 
 are moved up to the parent.   Committing this now, here's a patch:

 diff -r 653ae7f17379 examples/versioning/history_meta.py
 --- a/examples/versioning/history_meta.py       Mon Oct 17 13:14:04 2011 -0400
 +++ b/examples/versioning/history_meta.py       Mon Oct 17 13:19:41 2011 -0400
 @@ -60,6 +60,7 @@
          for column in local_mapper.local_table.c:
              if column.key not in super_history_mapper.local_table.c:
                  col = column.copy()
 +                col.unique = False
                  super_history_mapper.local_table.append_column(col)
          table = None

 this is rd87af238de36

 On Oct 17, 2011, at 12:56 PM, Damian wrote:







  Hello!

  I've run into an odd error whilst using the history meta.  I've got a
  class which has two sub classes which define some additional columns.
  The MainMeter class has a unique constraint on one of the colums.
  When I have the history_meta create my history tables, the
  trading_point_name column retains its unique constraint despite there
  being a col.unique = False in the history meta which should disable
  it.  I can't quite figure out why this is happening.  Any hints would
  be appreciated!

  class Meter(Base, BaseMixin):
     __metaclass__ = VersionedMeta
     __tablename__ = meters

     id = sa.Column(sa.Integer, primary_key=True)
     type = sa.Column(sa.Enum('main_meter', 'check_meter'))
     # and so forth

  class MainMeter(Meter):
     __mapper_args__ = {'polymorphic_identity': 'main_meter'}
     trading_point_name  = sa.Column(sa.Unicode(255), nullable=True,
  unique=True)
     #and so forth

  class CheckMeter(Meter):
     __mapper_args__ = {'polymorphic_identity': 'check_meter'}
     #etc

  Details:
  Sqlalchemy 7.3
  Sqlite db backend
  Python2.5

  Best regards,
  Damian

  --
  You received this message because you are subscribed to the Google Groups 
  sqlalchemy group.
  To post to this group, send email to sqlalchemy@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 sqlalchemy@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] Deletion

2011-10-18 Thread fribes
Hi all,

Despite some doc and web digging, I didn't find how to tell sqa to behave
the way I want :
on deletion on Peripheral, also delete in Actuator.

with the following code, the record in Actuator remains after a deletion,
and a subsequent creation fails with IntegrityError.

class Peripheral(Base):
__tablename__ = 'peripheral'
id = Column(Integer, primary_key=True)
label = Column(String(20), nullable=False)

__mapper_args__ = {'polymorphic_on': peripheral_type,
   'polymorphic_identity': 'peripheral'}

class Actuator(Peripheral):
__tablename__ = 'actuator'
__mapper_args__ = {'polymorphic_identity': 'actuator'}
id = Column(None, ForeignKey('peripheral.id'), primary_key=True)

duration = Column(Integer)

Any suggestion ?

Regards,

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@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] Cant understand behavior of sqlalchemy while adding same object twice in a session

2011-10-18 Thread Manav Goel
Hello,
   I am working in eclipse IDE. 
   I have this code in the file :


class User(Base):
__tablename__ = 'users'

id = Column('user_id',Integer,primary_key = True)
name = Column('user_name',String(20))

addresses = relationship(Address)

def __repr__(self):
return User(%s) % self.name
  
session = Session(bind=engine)
u=User('myname')
session.add(u)
session.flush()
print u.id

I ran this file and it worked fine but then I changed 
something. 
Now to see the effect I have to run file again and then I 
noticed this

Instead of adding two different objects with two different 
ids but same name. It simply updated the id of already existing object.

  Now I have two questions :

1. What is the reason of this behavior? First of all it should have 
made two different objects . But I think it did not do that because it thought 
this object is similar to existing one. In that case it should have raised 
exception instead of simply my primary key.

2. WHat happened when I ran the file again? It created a brand new 
session or continued the previous one?

3. Due to problem mentioned in point no 1. it wasted my early id nos as 
when I committed it saved the last one?

I know there is something that I am missing here as I am just a beginner in 
sqlalchemy.

Thanks in advance.

Regards,
Manav Goel

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@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] Deletion

2011-10-18 Thread Mike Conley
How are you doing the delete?

This should delete both.

a = sess.query(Peripheral).filter(Peripheral.label=='some label').one()
sess.delete(a)
sess.commit()


This will not work.

a = sess.query(Peripheral).filter(Peripheral.label=='some
label').delete()

I think the explanation here is that in this case we are creating an
explicit SQL delete statement without adding Peripheral instances to the
session and so there is no knowledge that a related Actuator exists. Someone
else might be able to give a better explanation.

-- 
Mike Conley



On Tue, Oct 18, 2011 at 9:03 AM, fribes fri...@gmail.com wrote:

 Hi all,

 Despite some doc and web digging, I didn't find how to tell sqa to behave
 the way I want :
 on deletion on Peripheral, also delete in Actuator.

 with the following code, the record in Actuator remains after a deletion,
 and a subsequent creation fails with IntegrityError.

 class Peripheral(Base):
 __tablename__ = 'peripheral'
 id = Column(Integer, primary_key=True)
 label = Column(String(20), nullable=False)

 __mapper_args__ = {'polymorphic_on': peripheral_type,
'polymorphic_identity': 'peripheral'}

 class Actuator(Peripheral):
 __tablename__ = 'actuator'
 __mapper_args__ = {'polymorphic_identity': 'actuator'}
 id = Column(None, ForeignKey('peripheral.id'), primary_key=True)

 duration = Column(Integer)

 Any suggestion ?

 Regards,

  --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@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 sqlalchemy@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] Using collation sequence in SqlAlchemy against Sqlite DB

2011-10-18 Thread Roja Sen
Could someone help me to set different collation in sqlAlchemy against
Sqlite DB?  I tried to execute something like SELECT
icu_load_collation('pl_PL', 'POLISH') when getting a connection but
couldn't
reference the collation sequence when creating the table object, I
keep
getting the following error.

*sqlite3.OperationalError: no such collation sequence: POLISH*

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@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] Deletion

2011-10-18 Thread Michael Bayer

On Oct 18, 2011, at 10:03 AM, fribes wrote:

 Hi all,
 
 Despite some doc and web digging, I didn't find how to tell sqa to behave the 
 way I want :
 on deletion on Peripheral, also delete in Actuator. 
 
 with the following code, the record in Actuator remains after a deletion, and 
 a subsequent creation fails with IntegrityError.
 
 class Peripheral(Base):
 __tablename__ = 'peripheral'
 id = Column(Integer, primary_key=True)
 label = Column(String(20), nullable=False)
 
 __mapper_args__ = {'polymorphic_on': peripheral_type,
'polymorphic_identity': 'peripheral'}
 
 class Actuator(Peripheral):
 __tablename__ = 'actuator'
 __mapper_args__ = {'polymorphic_identity': 'actuator'}
 id = Column(None, ForeignKey('peripheral.id'), primary_key=True)
 
 duration = Column(Integer)
 
 Any suggestion ?

usually relationship() with cascade=all, delete-orphan is used for this use 
case, so that SQLAlchemy can maintain knowledge about the link between 
Peripheral and Actuator.  The other alternative is to use ON DELETE CASCADE 
on the foreign key; this is part of the schema you'd generate in the database.

Relevant docs:

http://www.sqlalchemy.org/docs/orm/tutorial.html#configuring-delete-delete-orphan-cascade
http://www.sqlalchemy.org/docs/core/schema.html#on-update-and-on-delete
http://www.sqlalchemy.org/docs/orm/collections.html#using-passive-deletes



 
 Regards,
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@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 sqlalchemy@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] Cant understand behavior of sqlalchemy while adding same object twice in a session

2011-10-18 Thread Michael Bayer

On Oct 18, 2011, at 1:21 PM, Manav Goel wrote:

 Hello,
I am working in eclipse IDE. 
I have this code in the file :
 
 
   class User(Base):
   __tablename__ = 'users'
 
   id = Column('user_id',Integer,primary_key = True)
   name = Column('user_name',String(20))
 
   addresses = relationship(Address)
 
   def __repr__(self):
   return User(%s) % self.name
   
   session = Session(bind=engine)
   u=User('myname')
   session.add(u)
   session.flush()
   print u.id
 
 I ran this file and it worked fine but then I changed 
 something. 
 Now to see the effect I have to run file again and then I 
 noticed this
 
 Instead of adding two different objects with two 
 different ids but same name. It simply updated the id of already existing 
 object.
 
   Now I have two questions :
 
   1. What is the reason of this behavior? First of all it should have 
 made two different objects .

absolutely.   the User() object has no primary key assigned and is not the 
result of a previous query, so add() will add it as a new row.   So the code 
above is not the complete story and you'd have to figure out what you're doing 
on your end beyond what's illustrated above.

 But I think it did not do that because it thought this object is similar to 
 existing one.

This is not SQLA's behavior.  SQLAlchemy does not make guesses about data.

 In that case it should have raised exception instead of simply my primary key.

what kind of exception ?  its legal to have two objects with the same name here 
unless you applied a unique constraint to the user_name column which does not 
seem to be the case here.

 
   2. WHat happened when I ran the file again? It created a brand new 
 session or continued the previous one?

The Session is an instantiation of an object present in memory only within the 
Python process.  So by definition any new invocation of a Python program begins 
with all new state including Session objects.

 
 3. Due to problem mentioned in point no 1. it wasted my early id nos 
 as when I committed it saved the last one?

I don't know what this means.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@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] Using collation sequence in SqlAlchemy against Sqlite DB

2011-10-18 Thread Michael Bayer
I don't know what icu_load_collation is, but SQLite's documentation points to 
section 6.2, Collation Sequence Examples at 
http://www.sqlite.org/datatype3.html which describes how to create tables with 
certain collations.   There is also the COLLATE operator:  
http://www.sqlite.org/lang_expr.html#collateop .  Built-in collations described 
there include BINARY, NOCASE, and RTRIM, that's it.   

Your first step is to determine exactly what SQL you'd like to emit against 
your database using the sqlite3 command line utility.  Once you've identified 
the exact SQL conversation you'd like to have, it can be automated with 
SQLAlchemy through various means, possible features here include the func 
object as well as the collate function:

http://www.sqlalchemy.org/docs/core/expression_api.html#sqlalchemy.sql.expression.func
http://www.sqlalchemy.org/docs/core/expression_api.html?highlight=collate#sqlalchemy.sql.expression.collate






On Oct 18, 2011, at 1:54 PM, Roja Sen wrote:

 Could someone help me to set different collation in sqlAlchemy against
 Sqlite DB?  I tried to execute something like SELECT
 icu_load_collation('pl_PL', 'POLISH') when getting a connection but
 couldn't
 reference the collation sequence when creating the table object, I
 keep
 getting the following error.
 
 *sqlite3.OperationalError: no such collation sequence: POLISH*
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@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 sqlalchemy@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] How to keep ORM Sessions in sync when using the SQL expression language as well?

2011-10-18 Thread Russ
I often mix up the SQL expression language with the use of an ORM session, 
and it is great that SQLAlchemy more than supports this.

But... what are the recommended ways to keep the session in sync with what 
you do with the SQL expression stuff?

For example, with the ORM you can't really do a batch/bulk insert (that I 
know of), but you *can* mix in some SQL expression language to achieve it. 
 Here is a complete example that shows this for the standard Users/Addresses 
example:
http://pastebin.com/BArU6hci

In there I do a semi-bulk insert of addresses using both the ORM way and 
the SQL Expression Language way.  With the former way, the inserted 
addresses are available *with their ids* without ever seeming to have a 
query emitted to get them.  With the latter way (SQLEL) the ORM needed to 
emit a query to get the addresses (even when I don't ask for id), presumably 
to update it's identity map.

Although the latter behaviour isn't really surprising, can the query be 
avoided somehow?  Is there a way to manually update the session/identity-map 
with the info?

In general, is there a better way to do what I'm doing?  The example is 
obviously a simplified one... my actual use case is batch inserting 
thousands of records this way and I'd like to be able to keep the ORM and 
avoid the extra query if I could.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/rnzqnwQl858J.
To post to this group, send email to sqlalchemy@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.