[sqlalchemy] Re: Materialized Path for SQLAlchemy Declarative Base

2009-08-13 Thread allen.fowler



On Aug 13, 2:37 pm, Anton Gritsay gene...@angri.ru wrote:
 Hi, Allen!

 You can use something like this (yeah, I know that it isn't
 declarative in any way):

     class Node(Base):
         __tablename__ = 'node'
         id = Column(Integer, primary_key=True)
         parent_id = Column(ForeignKey('node.id'))

         parent = relation(Node, remote_side=[id])

         mp_path = Column(sqlamp.PathField())
         mp_depth = Column(sqlamp.DepthField())
         mp_tree_id = Column(sqlamp.TreeIdField())

     Node.mp = sqlamp.MPManager(
         Node.__table__,
         pk_field='id',
         parent_id_field='parent_id',
         path_field_name='mp_path',
         depth_field_name='mp_depth',
         tree_id_field_name='mp_tree_id'
     )
     Node.__mapper__.extension.append(Node.mp.mapper_extension)

 Note that you need to define mp_* colums. You doesn't need it if you
 create MPManager *before* creating mapper.




Anton,


Thank you for the help.  And the excellent library. :)

I will try this code out as soon as I can.

Michael B:
Is this the only way to register such an extension with declarative
base?   Can a simpler method be devised?

-- AF
--~--~-~--~~~---~--~~
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: VIEW alternative in SQLAlchemy

2009-08-09 Thread allen.fowler



On Aug 7, 11:45 am, Michael Bayer mike...@zzzcomputing.com wrote:
 AF wrote:

  Hello,

  I have a table of records in the database that I want to run read
  queries against, but I do want to include all of them in the search.
  (There are a couple of filtering parameters to exclude records from
  the searched pool, including an aptly named is_active flag.)

  Traditionally, I would think to put this in to a VIEW, and let the DB
  optimise out all the non-active records.

  Since SQLAlchemy does appear to support views, what is the correct way
  to handle this?  (Or did i miss it?)

  I am using declarative base.

 a view looks just like a table to SQLAlchemy.   the only thing that might
 not work is reflecting it.   otherwise you can just pretend its a Table
 (just can't flush to it).

After reading the other helpful messages in this thread, and upon
further reflection, it occurs to me that these various work arounds
all forgo one critical aspect that makes SQLAlchemy so useful.

Namely, the ability to use use SQLAlchey to fully create and populate
my database in a database agnostic manner.   For simple applications
like mine, this is a big part of keeping Easy Things Easy  Hard
Things Possible.

So, just to clarify:

At this point in time, can SQLAlchemy be used to define and query
simple VIEWs in a database agnostic manner?

And if not, is this a feature that is slated for addition any time
soon?

Thank you,
AF


--~--~-~--~~~---~--~~
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: Materialized Path for SQLAlchemy Declarative Base

2009-08-09 Thread allen.fowler

Werner,

On Aug 7, 12:36 pm, werner wbru...@free.fr wrote:
 Allen,

 allen.fowler wrote:

  On Aug 6, 6:54 pm, AF allen.fow...@yahoo.com wrote:

  Hello all,

  Has anyone here used the sqlamp: Materialized Path for SQLAlchemy
  library?

  I am wondering:

  1) Does it seem to work well?

  2) Did you use it with Declarative Base, and if so, how did you
  configure it?

  Anybody?

  Specifically, I am wondering about how adapt the sample code at:

 http://sqlamp.angri.ru/#quickstart

  ... so that it works with declarative base.

 I haven't used this library and I am no SA expert so take the following
 with a grain (or two) of salt.

 I would translate this:

 class Node(object):
     mp = sqlamp.MPManager(
         node_table, node_table.c.id, node_table.c.parent_id
     )

 To:
 class Node(Base):
     __table__ = sa.Table(u'node', metadata,
     sa.Column(u'id', sa.Integer(), sa.Sequence('gen_sample_id'), 
 primary_key=True, nullable=False),
     sa.Column(u'parent_id', sa.Integer(), sa.ForeignKey(u'node.id')),
 ...
     )

     mp = sqlamp.MPManager(
         __table__, __table__.c.id, __table__.c.parent_id)

 Werner

Thank you...

Though, that does not look at all like typical Declarative Base code
I've seen / been using.  Why the explicit assignment to __table__?

Further, where does the extension=[Node.mp.mapper_extension] binding
happen?

Thank you again,
AF
--~--~-~--~~~---~--~~
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: VIEW alternative in SQLAlchemy

2009-08-09 Thread allen.fowler



On Aug 9, 1:42 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Aug 9, 2009, at 1:24 PM, allen.fowler wrote:

  So, just to clarify:

  At this point in time, can SQLAlchemy be used to define and query
  simple VIEWs in a database agnostic manner?

  And if not, is this a feature that is slated for addition any time
  soon?

 CREATE VIEW is almost identical across backends and can be achieved  
 like this:

 someselect = select([table.c.foo, table.c.bar])
 eng = create_engine('...')
 eng.execute(CREATE VIEW foobar AS %s % someselect.compile(eng))

 then build yourself a Table with columns representing the view.

 its easy enough to build a CreateView DDL() construct in 0.6 to do  
 this, i.e.

 eng.execute(CreateView(myview, someselect))

 I'd consult the sqlalchemy.ext.compiler docs for how to do this.

 as a builtin I'm uncomfortable since it implies adding a View() object  
 to schema which raises lots of thorny questions like what if I  
 construct an INSERT against the view ?  what about materialized  
 views? , what if I join my View() to the underlying Table() ? etc.



I am still using 0.5, and am not familiar with below ORM usage of
SQLAlchemy, but I think I get the idea.

Still, though, it looses the auto generation capability via drop_all()/
create_all() vs. traditional tables and feels out-of-place along side
the rest of SQLAlchemy's slickness.

I do understand your concern about adding a View() object at this
stage.   Perhaps it could be implemented by limiting it's scope and
thinking of a better word to describe what is needed since it is not a
VIEW in the full meaning of the word. (SimpleView? PreSelected?)

Essentially, this simple case is just a pre-packaged select statement
upon which further refining select queries can be run.  The reason for
placing it in the DDL/ORM would be to ensure that: 1) It is clean to
implement at the Python level, and 2) That abstraction happens at the
DB level for both performance reasons and consumption by non-
SQLAlchemy readers.

I imagine that this would solve a great many use-cases for VIEWs in
smaller projects where SQLAlchemy's coolness is already so addictive.

Do you think such a solution is feasible?




--~--~-~--~~~---~--~~
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: Materialized Path for SQLAlchemy Declarative Base

2009-08-07 Thread allen.fowler



On Aug 6, 6:54 pm, AF allen.fow...@yahoo.com wrote:
 Hello all,

 Has anyone here used the sqlamp: Materialized Path for SQLAlchemy
 library?

 I am wondering:

 1) Does it seem to work well?

 2) Did you use it with Declarative Base, and if so, how did you
 configure it?


Anybody?

Specifically, I am wondering about how adapt the sample code at:

http://sqlamp.angri.ru/#quickstart

... so that it works with declarative base.


Thank you,
:)


--~--~-~--~~~---~--~~
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: VIEW alternative in SQLAlchemy

2009-08-07 Thread allen.fowler



On Aug 7, 11:45 am, Michael Bayer mike...@zzzcomputing.com wrote:
 AF wrote:

  Hello,

  I have a table of records in the database that I want to run read
  queries against, but I do want to include all of them in the search.
  (There are a couple of filtering parameters to exclude records from
  the searched pool, including an aptly named is_active flag.)

  Traditionally, I would think to put this in to a VIEW, and let the DB
  optimise out all the non-active records.

  Since SQLAlchemy does appear to support views, what is the correct way
  to handle this?  (Or did i miss it?)

  I am using declarative base.

 a view looks just like a table to SQLAlchemy.   the only thing that might
 not work is reflecting it.   otherwise you can just pretend its a Table
 (just can't flush to it).

To clarify:

I am using SQLAlchemy's Declarative Base to fully define and create my
database.

For instance, there is a simple class/table Records, and I would like
to define a class CurrentRecords that is implemented in the database
as a view on Records.

In this way, I can avoid polluting my application code with filtering
out all the non-active records every time I want to query Records.

Thank you,
:)





--~--~-~--~~~---~--~~
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: VIEW alternative in SQLAlchemy

2009-08-07 Thread allen.fowler


 ... To clarify:

  I am using SQLAlchemy's Declarative Base to fully define and create my
  database.

  For instance, there is a simple class/table Records, and I would like
  to define a class CurrentRecords that is implemented in the database
  as a view on Records.

  In this way, I can avoid polluting my application code with filtering
  out all the non-active records every time I want to query Records.

 Just define CurrentRecords as a table, i.e. in my app one of my views is:

 class Vconsumption(Base):
     __table__ = sa.Table(u'vconsumption', metadata,
     sa.Column(u'consumptionid', sa.Integer(),
 sa.ForeignKey(u'consumption.consumptionid'), primary_key=True),
 ...
     )

     consumption = sao.relation(Consumption)

 And I relate it back to the real consumption table, but never
 write/flush the view and do have to have a unique key which you define
 as primary key to SA.

 Werner

What functional gain does this approuch provide over just querying the
Consumption table?  I am not clear on how you are using this  can
you clarify?

As an aside, I wonder if it is possible to just subclass my Records
object so that the CurrentRecords class adds/enforces certain
filter_by parameters for any query against it.

Thank you.  :)



--~--~-~--~~~---~--~~
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: Relation w/ declarative

2009-08-06 Thread allen.fowler



On Aug 6, 3:30 am, werner wbru...@free.fr wrote:

 I never do this, i.e. I assign like this
 add = Address()
 add.email_address = 'an email address' In what way is the Address object 
 expected to be instantiated such
  that it receives the correct user id?

 You just do this and SA will take care of the id's:
 con = db.Contact()
 con.name = 'a name'

 add = db.Address()
 add.street = 'a street'

 con.address.append(add)

 session.add(con)
 session.add(add)
 session.commit()


OK, that explains it.  Thank you.



 Above is based on this model (db) and the BaseExt was a tip I had from
 Michael.

 class BaseExt(object):
     def __repr__(self):
         return %s(%s) % (
                  (self.__class__.__name__),
                  ', '.join([%s=%r % (key, getattr(self, key))
                             for key in sorted(self.__dict__.keys())
                             if not key.startswith('_')]))

 Base = sad.declarative_base(cls=BaseExt)
 metadata = Base.metadata


What is that __repr__ function doing exactly?  Looks interesting.

--~--~-~--~~~---~--~~
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: Relation w/ declarative

2009-08-06 Thread allen.fowler



On Aug 6, 4:59 am, Wichert Akkerman wich...@wiggy.net wrote:
 On 8/6/09 09:30 , werner wrote:

  IIRC correctly the __init__ section is only needed if you want to do:
  add = Address('an email address')

  I never do this, i.e. I assign like this
  add = Address()
  add.email_address = 'an email address'

 You can also do this with the default declarative base constructor:

    add = Address(email_address='j...@example.com')

 All columns can be used as named parameters.


Brilliant! :)




--~--~-~--~~~---~--~~
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: Suggestions for db connect string configuration?

2009-08-06 Thread allen.fowler



On Aug 5, 6:29 pm, AF allen.fow...@yahoo.com wrote:
 Hello,

 Where do you folks recommend storing the database connection string in
 my application.  Clearly not in the same file with my declaratively
 defined model objects.

 And more generally, how do you recommend laying out an SQLAlchemy
 based application?  (In what files to define the engine, session,
 other objects, etc..)

 Sort of looking for best practices, I guess

 Thank you,
 :)


Anybody?


Also, if using declarative base, is there any problem adding my own
methods to the objects?
--~--~-~--~~~---~--~~
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: Declarative Base: remote_side single_parent

2009-08-06 Thread allen.fowler



On Aug 6, 4:05 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 allen.fowler wrote:

  I tried:

  children = relation(Node, backref=backref(parent,
  remote_side=nodes.id))

  got it to work with:

  remote_side=[id]

  But:

  1) Why is remote_side a list?

 in this case you could just say remote_side=id.   its optionally a list if
 multiple columns occur on the remote side of the join condition (i.e. as
 in a composite primary key).

  2) Where does single_parent fit in to this?

 single_parent is a flag that gets suggested to you if you attempt to use
 delete-orphan cascade with a many-to-one or many-to-many foreign key
 combination (meaning they are essentially distilled into one-to-one or
 one-to-many).  it's basically requiring you to sign the agreement I
 promise not to connect this object to more than one parent of this type so
 that delete-orphan does what's advertised.   if it hasn't been suggested
 to you, there's no need to use it (unless you want to enforce that
 contract otherwise).

Thank you for the clarification, Mike.

--~--~-~--~~~---~--~~
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: Hierarchical data: Get all (sub-) children? Parents?

2009-08-05 Thread allen.fowler



On Jul 28, 12:17 pm, David Gardner dgard...@creatureshop.com wrote:
 Just thought I would toss in my 2-cents here, since I have lots of
 hierarchical data and have
 at one time or another used most of the below methods.

 Choice #1 is the option that I have found that works the best.
 I Use a file path-like primary key (actually I am storing data about
 files), this allows me to easily do
 things like:
 nodes=session.query(Node).filter(Node.path.like('root/path/sub/%')).order_by(Node.path).all()

 Choice #3 works well with Choice #1, especially if you are interested in
 a particular node, and know ahead of time
 you also want that node's grandparent.



I've read up a bit on the suggested links.  Thank you for the leads.


If I am understanding this, it seems that choice #1 is very much like
the materialised path method as mentioned in:

http://sqlamp.angri.ru/  , and
http://www.dbazine.com/oracle/or-articles/tropashko4

I will experiment some and report back. (My max depth is about 10
levels with under 1000 total nodes.)


Thank you,
:)


--~--~-~--~~~---~--~~
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: SQLAlchemy as a FIFO buffer?

2009-06-30 Thread allen.fowler



On Jun 30, 11:25 am, Didip Kerabat did...@gmail.com wrote:
 If you are open to non RDBMS solution, sounds like what you need is message
 queue system.

 At work, we use RabbitMQ (memory-only) and have been quite happy with it.

 SecondLife posted their discovery about MQ 
 here:http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes


RabbitMQ looks very nice.  However, one of the requirements is to be
able run with out any additional resident daemons.   All system
actions are triggered by cron, CGI, and locally SSH executed scripts.

RabbitMQ may make it in to future experiments.
--~--~-~--~~~---~--~~
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: SQLAlchemy as a FIFO buffer?

2009-06-30 Thread allen.fowler


 I've attached a proof of concept for how I've approached the homegrown
 version of this in the past.   a jobs table has two update passes - one to
 atomically mark jobs as in progress by a certain procid, then the
 transaction is released so that other processes can theoretically work on
 other jobs.  after each job is completed the corresponding row is marked
 as complete (or failed) within a separate short transaction per row.  
 no long running transactions are used so that there is always room for
 multiple processes to join in the work.   Oftentimes you'll hear people
 doing this with MySQL and dealing with select...for update to try to
 lock the rows as they are selected within the transaction but that method
 has never appealed to me.

 all that said, I'm sure my approach has issues that are not (or perhaps
 are) readily apparent.   if you want really failproof behavior without
 much tinkering the off the shelf solutions are probably worth a look.


Thank you.. I am looking at the code now.  Having not done any
threading or post orm tutorial SQL Alchemy programming, I am not
sure what it all does.

Quick questions:
How doe this code handle transactions?
Where in the code does it atomically assign a task to a worker without
making a race condition?   (Or does it?)

Thank you,
:)
--~--~-~--~~~---~--~~
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: SQLAlchemy as a FIFO buffer?

2009-06-29 Thread allen.fowler


  I have several CGI and cron scripts and that I would like coordinate
  via a First In / First Out style buffer.    That is, some processes
  are adding work units, and some take the oldest and start work on
  them.

  Since I need the queue to both survive system crashes and provide an
  audit record of all work-units in/out, people have suggested using an
  ACID compliant databse for the task. (SQLite or MySQL depending on
  volume).

  Can SQLAlchemy be used for the task?

  Any help would be appreciated, as I a new to SQL and SQLAlchemy.

 Sounds reasonable and fairly simple to do.

 I think you would need two tables.
 One for the tasks to be done an another for tasks completed (the
 history you wanted)

 It sounds like you would need the following functionality
 * Insert record - Adding records to the que
 * Qurey record - Finding the record that needs doing.
 * Deleting record - Remove the task from the current_tasks table when
 the task is complete.

 I'd use the current date+time as a key for the current_tasks table so
 you can query and return the first (session.queryfirst(), not all
 ()) record with the lowest date (as you may occasionally have more
 than one entry with the exact same date)

 SQLAlchemy is defiantly capable of doing all of these things and
 provides awesome ORM functionality so you don't have to construct any
 SQL - Look at declarative base
 rough example here 
 :http://www.sqlalchemy.org/docs/05/ormtutorial.html#creating-table-cla...
 more details here 
 :http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html

 Good luck.



OK...  I am going to try implement something like this using SQA 
sqlite

(Has anyone done this FIFO buffer / message channel system in SQL
Alchemy before?  I hate to re-invent the wheel.)

The first problem will am grappling with is a way to atomically
retrive an item from the queue.

After some asking around on the sqlite mailing list:
http://www.nabble.com/forum/ViewPost.jtp?post=23590920framed=y

It seems the solution is to use a BEGIN EXCLUSIVE TRANSACTION;  to
warp the parts of the code that do the FIFO stuff.

How would I do this in SQLAlchemy?  (With out affecting the rest of my
code that uses the regular session/transaction system.)

Thank you
:)
--~--~-~--~~~---~--~~
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: Validators: Define at table / mapper level ?

2009-06-23 Thread allen.fowler


 If you have mapper definitions separate from classes, theres nothing
 stopping you from adding attributes to the class over there, i.e.

 mapper(MyClass, mytable)

 MyClass.foo = some_validation_decorator(MyClass.foo)

 Personally I wouldn't bother (then again I use declarative for everything
 now).



Perhaps I should try declarative... are there any gotcha's to know
about?


--~--~-~--~~~---~--~~
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: filtered or computed version of an existing relation?

2009-06-21 Thread allen.fowler


Thank you, Michael.

On Jun 18, 9:41 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On Jun 18, 2009, at 2:27 AM, AF wrote:



  OK, next question.

  Well... two related questions.  :)

  1)  In general, inside an object's method def, where I am doing
  arbitrary calculations, how can I get access to the session the object
  is bound to in order to run other queries?

 object_session(self)

  2) More specifically, I want to make a filtered and computed
  version of an existing relation, how should I do this?

 I would use object_session(self).query(Class)... to create the  
 appropriate query.
--~--~-~--~~~---~--~~
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: Random value for field by default?

2009-06-21 Thread allen.fowler

OK, never mind... I solved it.

The default = random.randrange(1000,1) code was happily taking the
static return value.   Duh.

I changed it to:

default = lambda: random.Random().randrange(2000,8000)

I dunno if the extra Random() is needed, but it can't hurt, right?



On Jun 21, 4:32 pm, AF allen.fow...@yahoo.com wrote:
 Hello,

 Perhaps this is more of a Python question that SQLalchemy.. but...

 How can I assign a random number to a DB field by default?

 I tried:
 default = random.randrange(1000,1) on the table definition, but I
 get the same number each time?

 Ideas?
--~--~-~--~~~---~--~~
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: Random value for field by default?

2009-06-21 Thread allen.fowler


 default = lambda: random.randrange(1000,1)


Seems we crossed in the interwebs.. :)

Is it safe to do this, or do you need to do  default = lambda:
random.Random()randrange(1000,1) ?

I ask since I have several tables that this needs to be applied to.

Thank you
--~--~-~--~~~---~--~~
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: relation error?

2009-06-17 Thread allen.fowler


 3) Can this relation's objects be made read-only?  That is:
 u.room = new_room would work, but this would not:  u.room.name =
 'kitchen'


To clarify the question:

Can this objects seen via this relation be made read-only?  That is:

u.room = a_room
u.room = a_new_room
...would work fine.

And this would work, too:
r = session.query(u.room).one()
r.name = kitchen

But, this would not:
u.room.name = 'kitchen'

--~--~-~--~~~---~--~~
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: relation error?

2009-06-17 Thread allen.fowler


 you can, you can use a validator that rejects all changes, or if
 you're
 brave you can create a custom __setattribute__ method that brokers all
 attribute setter access.   the former is in the SQLA mapping docs the
 latter is part of Python.

Thank you.  Interesting seems a bit of a round about, though.  :)
--~--~-~--~~~---~--~~
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: Can I coerce strings into Unicode?

2009-06-12 Thread allen.fowler

Anybody?

On Jun 4, 1:13 am, AF allen.fow...@yahoo.com wrote:
 Hello,

 I'm using sqlite and convert_unicode = True on the engine.

 How can I force coerce string based object attributes in to unicode?
 (I had thought convert_unicode = True would do this)

 Here is what I am seeing...

 Setup code:
 engine = create_engine('sqlite:///:memory:', echo=True,
 convert_unicode=True)
 Session = sessionmaker(bind=engine)
 session = Session()
 metadata = MetaData()
 m1 = message(u'message body 1')

 Now, in ipython:

 In [1]: session.add(m1)

 In [2]: m1.body
 Out[2]: u'message body 1'

 In [3]: m1.body = u'new - unicode'

 In [4]: m1.body
 Out[4]: u'new - unicode'

 In [5]: m1.body = 'new - NOT unicode'

 In [6]: m1.body
 Out[6]: 'new - NOT unicode'

 In [7]: unicode(m1.body)
 Out[7]: u'new - NOT unicode'

 Output line 6 is the problem.

 Ideally, I'd like to see output lines 6  7 be the same.

 Am I doing something wrong?

 Thank you,
 Allen
--~--~-~--~~~---~--~~
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: Can I coerce strings into Unicode?

2009-06-12 Thread allen.fowler



On Jun 12, 6:00 am, Gunnlaugur Briem gunnlau...@gmail.com wrote:
 The engine's conversion to unicode doesn't happen when you assign the
 property, it happens when the underlying database operation is
 committed, and arrives in the python object's property only after
 roundtripping through the database.


OK, i see.

Are there any shortcuts for installing a filter on the object to
mandate (and force if possible) UTF-8 on all incoming property
assignments?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---