[sqlalchemy] Weird mapper() behaviour (bug?)

2009-03-02 Thread Karlo Lozovina

Hi guys! Here goes the code sample:


from sqlalchemy import create_engine, MetaData
from sqlalchemy import Table, Column, ForeignKey, Integer
from sqlalchemy.orm import sessionmaker, mapper, relation

metadata = MetaData()
engine = create_engine('sqlite:///:memory:', echo=True)

parent_table = Table('parent', metadata,
Column('id', Integer, primary_key=True))
child_table = Table('child', metadata,
Column('id', Integer, primary_key=True),
Column('parent_id', Integer, ForeignKey('parent.id')))

class Parent(object): pass
class Child(object): pass

mapper(Parent, parent_table, properties={'children': relation(Child,
backref=u'parent')})
mapper(Child, child_table)
metadata.create_all(engine)
session = sessionmaker(bind=engine)()
parentobj = Parent()


Running this code fails with this error:

.../SQLAlchemy-0.5.2-py2.6.egg/sqlalchemy/orm/properties.pyc in
_post_init(self)
993
994 if self.backref is not None:
-- 995 self.backref.compile(self)
996 elif not mapper.class_mapper(self.parent.class_,
compile=False)._get_property(self.key, raiseerr=False):
997 raise sa_exc.ArgumentError(Attempting to assign a
new relation '%s' to 
AttributeError: 'unicode' object has no attribute 'compile'

This line is the problem:
mapper(Parent, parent_table, properties={'children': relation
(Child, backref=u'parent')})
or, to be precise, `backref=u'parent'`. With just `backref='parent'`,
everything works just fine, it breaks down when using a unicode
literal for backref name. Is this a bug, or somehow expected behavior?

Thanks everyone...
--~--~-~--~~~---~--~~
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] Re: Weird mapper() behaviour (bug?)

2009-03-02 Thread Karlo Lozovina

On Mar 3, 1:03 am, Michael Bayer mike...@zzzcomputing.com wrote:

 its a bug, there's an isinstance(str) that should be  
 isinstance(basestring).  please file a trac ticket.

Done, here it is:
http://www.sqlalchemy.org/trac/ticket/1330

--
Karlo Lozovina
--~--~-~--~~~---~--~~
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] Properly closing all database connections

2008-05-06 Thread Karlo Lozovina

I'm using SA with SQLite, and after executing session.close() and
clear_mappers(), on Linux, lsof still says my SQLite file is open.
Running engine.dispose() seems to finally close it, but I'm not sure
if that's the proper way?

Thanks,
Karlo.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Properly closing all database connections

2008-05-06 Thread Karlo Lozovina

On May 6, 10:53 pm, jason kirtland [EMAIL PROTECTED] wrote:

 That's correct.  The engine holds the connection pool and dispose() will
 close the connections it manages.

Thanks... it's weird there's no mention of it in the docs.

--
Karlo Lozovina - Mosor
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Duplication of rows in many-to-many relationship

2008-05-04 Thread Karlo Lozovina

Let's say I have two classes A and B, and I want instances of both
classes, to have a list of each other, that is, many-to-many
relationship. For a shorthand, a means instance of A, and b is an
instance of B.

For example: a.bs is a list, full of instances of class B.
Similarly, b.as is a list, full of instances of class A. In
modelling that relationship I use three tables, one for As, one for
Bs, and one for their relationship. If I only append instances of B to
some a.bs, then save all those objects, everything works fine. But
if I append instances of A and B, both to a.bs and b.as, then
save, I get double rows in the third table. Is there a way around
that?

P.S.
In a very likely case I haven't been completely understood, I'll
attach some code to demonstrate my point ;).

Thanks all.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Duplication of rows in many-to-many relationship

2008-05-04 Thread Karlo Lozovina

On May 4, 10:40 pm, Barry Hart [EMAIL PROTECTED] wrote:

 By chance, in your mappers, are you declaring two relationships instead of 
 one relation with a
 backref?

Yep, that was it, thanks! Should have read the docs more carefully ;).

 As a side note, once you straighten this out, you may want to declare the 
 composite (a_id, b_id)
 as a unique key on the relation table.

I'll look into it...

Thanks for a speedy reply ;).
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Linking column default values?

2007-11-21 Thread Karlo Lozovina

Hi guys, sorry for the vague subject, here is my problem. Let's say I
have the following table:

a = Table('aaa', meta,
  Column('id', Integer, primary_key=True),
  Column('id2', Integer))

class A(object):
  pass

mapper(A, a)

I want the default value of A.id2 to be that of a A.id. So, for
example, unless I modify it, I would like the A.id2 to be sort of a
duplicate primary key. I know I can find out what is A.id after
committing my object to database and then setting A.id2 accordingly,
but I was wondering can SA do this automatically for me? Maybe some
kind of a special `default=` construct in the Column call?

Thanks,
Karlo.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] SQLite and ON CONFLICT clause?

2007-03-19 Thread Karlo Lozovina

Hi all,

browsing the SQLAlchemy docs I coudn't find any references to a
particular SQLite feature, ON CONFLICT clause (http://www.sqlite.org/
lang_conflict.html), is there any support for this in SA? What I want
to do is this, create my database as such:

CREATE TABLE songs(
id INTEGER PRIMARY KEY ON CONFLICT REPLACE,
name TEXT
);

Now, when I do a bunch of INSERTs without specifying 'id' field,
everything works as you would expect. But, when I issue a INSERT with
a 'id' that allready exists, SQLite automatically and automagicaly
does an UPDATE instead.

Now, this is very handy because I don't have to do a SELECT prior to
modifying a table. Is there a way to use this SQLite feature from SA?
Or somehow simulate it without too much gory details?

Thanks guys,
klm.


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] SA support for SQLite ATTACH?

2007-03-09 Thread Karlo Lozovina

Greetings everyone,

does SQLAlchemy somehow support SQLites' ATTACH DATABASE statement? I
have a in-memory SQLite database that I want to dump to file, so I was
thinking of using ATTACH to do it. Any other ideas welcome ;).

Thanks in advance,
Karlo.


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: SA support for SQLite ATTACH?

2007-03-09 Thread Karlo Lozovina

On Mar 9, 4:33 pm, Michael Bayer [EMAIL PROTECTED] wrote:

 attach database ...wow i never knew it had that !  if its a matter of
 issuing the string attach database, just use literal text() or
 engine.execute().

Me neither ;), until the other day I started looking for an efficient
way to dump in-memory SQLite databases to hdd. And this ATTACH thing
sounded just like made for it.

 attach databasevery handy !

So, let's say I issue this engine.execute(), how would one proceede
inserting rows into this attached database. Let's say mem_db is the
main, in-memory database, and file_db has just been attached, and is a
in-file database. SQL syntax is somewhat like this: INSERT INTO
file_db.table1 SELECT * FROM mem_db.table1, is any way SQLAlchemy can
do this without resorting to engine.execute() ?

Thanks!

Karlo.


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---