[sqlalchemy] Elixir question

2012-02-03 Thread lars van gemerden
Hi, I am trying to sote pairs in a table as follows:

#--
from elixir import *

metadata.bind = sqlite:///:memory:
metadata.bind.echo = False

class Pairs(Entity):
name = Field(String(50), primary_key = True)
other = OneToOne('Pairs', inverse = 'other')

def __init__(self, name):
self.name = name

def pair(name1, name2):
p1, p2 = Pairs(name1), Pairs(name2)
p1.other = p2

if __name__ == '__main__':

setup_all()
create_all()

pair('p1', 'p2')
#--

I am not very famiiar with SQL etc. but logically this seems possible
(please orrect me if I am wrong). However I get the following error:

File D:\Documents\Code\Eclipse\workspace\SQLAdata\src\tests.py, line
22, in module
setup_all()
  File build\bdist.win-amd64\egg\elixir\__init__.py, line 94, in
setup_all

  File build\bdist.win-amd64\egg\elixir\entity.py, line 951, in
setup_entities
  File build\bdist.win-amd64\egg\elixir\entity.py, line 198, in
create_pk_cols
  File build\bdist.win-amd64\egg\elixir\entity.py, line 481, in
call_builders
  File build\bdist.win-amd64\egg\elixir\relationships.py, line 448,
in create_pk_cols
  File build\bdist.win-amd64\egg\elixir\relationships.py, line 791,
in create_keys
  File build\bdist.win-amd64\egg\elixir\relationships.py, line 521,
in inverse
AssertionError: Relationships 'other' in entity 'Pair' and 'other' in
entity 'Pair' cannot be inverse of each other because their types do
not form a valid combination.


Can anyone help me to understand the error and possibly fix it?

Thanks in advance, Lars

-- 
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: Elixir question

2012-02-03 Thread lars van gemerden
I should probably make the pair method:

def pair(name1, name2):
p1, p2 = Pairs(name1), Pairs(name2)
p1.other = p2
p2.other = p1

On Feb 3, 11:57 am, lars van gemerden l...@rational-it.com wrote:
 Hi, I am trying to sote pairs in a table as follows:

 #-- 
 
 from elixir import *

 metadata.bind = sqlite:///:memory:
 metadata.bind.echo = False

 class Pairs(Entity):
     name = Field(String(50), primary_key = True)
     other = OneToOne('Pairs', inverse = 'other')

     def __init__(self, name):
         self.name = name

 def pair(name1, name2):
     p1, p2 = Pairs(name1), Pairs(name2)
     p1.other = p2

 if __name__ == '__main__':

     setup_all()
     create_all()

     pair('p1', 'p2')
 #-- 
 

 I am not very famiiar with SQL etc. but logically this seems possible
 (please orrect me if I am wrong). However I get the following error:
 
 File D:\Documents\Code\Eclipse\workspace\SQLAdata\src\tests.py, line
 22, in module
     setup_all()
   File build\bdist.win-amd64\egg\elixir\__init__.py, line 94, in
 setup_all

   File build\bdist.win-amd64\egg\elixir\entity.py, line 951, in
 setup_entities
   File build\bdist.win-amd64\egg\elixir\entity.py, line 198, in
 create_pk_cols
   File build\bdist.win-amd64\egg\elixir\entity.py, line 481, in
 call_builders
   File build\bdist.win-amd64\egg\elixir\relationships.py, line 448,
 in create_pk_cols
   File build\bdist.win-amd64\egg\elixir\relationships.py, line 791,
 in create_keys
   File build\bdist.win-amd64\egg\elixir\relationships.py, line 521,
 in inverse
 AssertionError: Relationships 'other' in entity 'Pair' and 'other' in
 entity 'Pair' cannot be inverse of each other because their types do
 not form a valid combination.
 

 Can anyone help me to understand the error and possibly fix it?

 Thanks in advance, Lars

-- 
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] a more general elixir question

2012-02-03 Thread lars van gemerden
Is it possible to mix Elixir classes and SQLA classes in one database
(with foreign keys between them)?

Cheers, Lars

-- 
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: a more general elixir question

2012-02-03 Thread erikj
yes

Elixir classes are in fact SQLA classes, only defined with
a different syntax

you can mix Elixir, Declarative and plain python objects mapped
with SQLA

On Feb 3, 12:13 pm, lars van gemerden l...@rational-it.com wrote:
 Is it possible to mix Elixir classes and SQLA classes in one database
 (with foreign keys between them)?

 Cheers, Lars

-- 
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: a more general elixir question

2012-02-03 Thread lars van gemerden
Thanks,

Can you also mix Elixir Fields and SQLA Column/relationships in the
same class?

Cheers, Lars

On Feb 3, 12:56 pm, erikj tw55...@gmail.com wrote:
 yes

 Elixir classes are in fact SQLA classes, only defined with
 a different syntax

 you can mix Elixir, Declarative and plain python objects mapped
 with SQLA

 On Feb 3, 12:13 pm, lars van gemerden l...@rational-it.com wrote:







  Is it possible to mix Elixir classes and SQLA classes in one database
  (with foreign keys between them)?

  Cheers, Lars

-- 
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: a more general elixir question

2012-02-03 Thread erikj
no, that's not possible

Elixir does some custom postprocessing on a class
definition, and so does Declarative, but they are not
interoperatable

On Feb 3, 1:20 pm, lars van gemerden l...@rational-it.com wrote:
 Thanks,

 Can you also mix Elixir Fields and SQLA Column/relationships in the
 same class?

 Cheers, Lars

 On Feb 3, 12:56 pm, erikj tw55...@gmail.com wrote:

  yes

  Elixir classes are in fact SQLA classes, only defined with
  a different syntax

  you can mix Elixir, Declarative and plain python objects mapped
  with SQLA

  On Feb 3, 12:13 pm, lars van gemerden l...@rational-it.com wrote:

   Is it possible to mix Elixir classes and SQLA classes in one database
   (with foreign keys between them)?

   Cheers, Lars

-- 
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: a more general elixir question

2012-02-03 Thread lars van gemerden
Too bad, thanks

On Feb 3, 1:38 pm, erikj tw55...@gmail.com wrote:
 no, that's not possible

 Elixir does some custom postprocessing on a class
 definition, and so does Declarative, but they are not
 interoperatable

 On Feb 3, 1:20 pm, lars van gemerden l...@rational-it.com wrote:







  Thanks,

  Can you also mix Elixir Fields and SQLA Column/relationships in the
  same class?

  Cheers, Lars

  On Feb 3, 12:56 pm, erikj tw55...@gmail.com wrote:

   yes

   Elixir classes are in fact SQLA classes, only defined with
   a different syntax

   you can mix Elixir, Declarative and plain python objects mapped
   with SQLA

   On Feb 3, 12:13 pm, lars van gemerden l...@rational-it.com wrote:

Is it possible to mix Elixir classes and SQLA classes in one database
(with foreign keys between them)?

Cheers, Lars

-- 
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] Having trouble getting a subquery to return entities

2012-02-03 Thread cbc
Hi:

I'm having trouble getting a subquery to return entities.

class Question(Base):
__tablename__ = 'question'
question_id = Column(INTEGER(unsigned=True), primary_key=True)
...etc
q_answers = relationship(Answer, backref=question)

class Answer(Base):
__tablename__ = 'answer'
answer_id = Column(INTEGER(unsigned=True), primary_key=True)
user_id = Column(INTEGER(unsigned=True), ForeignKey('user.user_id'),
nullable=False)
question_id = Column(INTEGER(unsigned=True),
ForeignKey('question.question_id'), nullable=False)
...etc

stmt = session.query(Answer).filter(Answer.user_id ==
user_id).subquery()
answers = aliased(Answer, stmt)
query = session.query(Question, answers)\
.outerjoin(answers, Question.q_answers)\
.filter(Question.question_group_id == question_group_id)
questions = query.all()

This generates MySQL that returns all desired columns and returns NULL
if question has not yet been answered by the specified user. Groovy so
far.

I was expecting tuples in the list of questions (Answer, Question),
but the first element is always None. e.g.

 dir(questions[0])
[None, 'Question', ... etc

So while I'm expecting the subquery results to be understood as
entities (Answer), this isn't happening. But I cannot figure out why.

Where have I failed?

Thanks!

-C

-- 
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: Having trouble getting a subquery to return entities

2012-02-03 Thread cbc
I should add: First element is always none _even if question has been
answered by that user_

On Feb 3, 11:28 am, cbc clayton.cafi...@mondaylambson.com wrote:
 Hi:

 I'm having trouble getting a subquery to return entities.

 class Question(Base):
         __tablename__ = 'question'
         question_id = Column(INTEGER(unsigned=True), primary_key=True)
         ...etc
         q_answers = relationship(Answer, backref=question)

 class Answer(Base):
         __tablename__ = 'answer'
         answer_id = Column(INTEGER(unsigned=True), primary_key=True)
         user_id = Column(INTEGER(unsigned=True), ForeignKey('user.user_id'),
 nullable=False)
         question_id = Column(INTEGER(unsigned=True),
 ForeignKey('question.question_id'), nullable=False)
         ...etc

 stmt = session.query(Answer).filter(Answer.user_id ==
 user_id).subquery()
 answers = aliased(Answer, stmt)
 query = session.query(Question, answers)\
         .outerjoin(answers, Question.q_answers)\
         .filter(Question.question_group_id == question_group_id)
 questions = query.all()

 This generates MySQL that returns all desired columns and returns NULL
 if question has not yet been answered by the specified user. Groovy so
 far.

 I was expecting tuples in the list of questions (Answer, Question),
 but the first element is always None. e.g.

  dir(questions[0])

 [None, 'Question', ... etc

 So while I'm expecting the subquery results to be understood as
 entities (Answer), this isn't happening. But I cannot figure out why.

 Where have I failed?

 Thanks!

 -C

-- 
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] update table record

2012-02-03 Thread Mason
Hi,

If i have a table named 'message' and it has the following columns:
message_id, tar_del, src_del.  I pass in del_list which is a list of
message to delete.  I tried the following

self.session.query(Message)\
 .filter(Message.message_id.in_(del_list))\
 .update({Message.tar_del: self._DELETE})

but kept getting this error

  File /usr/lib64/python2.7/site-packages/sqlalchemy/orm/query.py,
line 2598, in update
Could not evaluate current criteria in Python. 
InvalidRequestError: Could not evaluate current criteria in Python.
Specify 'fetch' or False for the synchronize_session parameter.

What does the error mean?  What I ended up doing is

 rows = self.session.query(Message)\
.filter(Message.message_id.in_(del_list))\
  .all()

 for r in rows:
 if key == 'src_del'
 r.src_del = self._DELETE
 elif key == 'tar_del'
 r.tar_del = self._DELETE

and just loop through the rows return, but I think this is not very
efficient.

Also, to reference a table column, I do r.src_del and r.tar_del, is it
possible to make it a variable, so I can do something like r[key] =
self._DELETE?

Thanks,
Mason

-- 
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 a standalone SEQUENCE

2012-02-03 Thread Michael Hipp
Is there an example or other explanation of how to use a SEQUENCE that is not a 
primary key on a table, particularly how to create the sequence, get next 
value, reset seq, etc. I see the base docs here, but it's not obvious to me how 
exactly to use this class.


http://docs.sqlalchemy.org/en/latest/core/schema.html#sqlalchemy.schema.Sequence

Thanks,
Michael Hipp

--
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] Having trouble getting a subquery to return entities

2012-02-03 Thread Michael Bayer

On Feb 3, 2012, at 11:28 AM, cbc wrote:

 Hi:
 
 I'm having trouble getting a subquery to return entities.
 
 class Question(Base):
   __tablename__ = 'question'
   question_id = Column(INTEGER(unsigned=True), primary_key=True)
   ...etc
   q_answers = relationship(Answer, backref=question)
 
 class Answer(Base):
   __tablename__ = 'answer'
   answer_id = Column(INTEGER(unsigned=True), primary_key=True)
   user_id = Column(INTEGER(unsigned=True), ForeignKey('user.user_id'),
 nullable=False)
   question_id = Column(INTEGER(unsigned=True),
 ForeignKey('question.question_id'), nullable=False)
   ...etc
 
 stmt = session.query(Answer).filter(Answer.user_id ==
 user_id).subquery()
 answers = aliased(Answer, stmt)
 query = session.query(Question, answers)\
.outerjoin(answers, Question.q_answers)\
.filter(Question.question_group_id == question_group_id)
 questions = query.all()
 
 This generates MySQL that returns all desired columns and returns NULL
 if question has not yet been answered by the specified user. Groovy so
 far.
 
 I was expecting tuples in the list of questions (Answer, Question),
 but the first element is always None. e.g.
 
 dir(questions[0])
 [None, 'Question', ... etc
 
 So while I'm expecting the subquery results to be understood as
 entities (Answer), this isn't happening. But I cannot figure out why.
 
 Where have I failed?

The outerjoin here is from Question to Answer so you should never have None in 
the first element of the tuple.  I'd need a full reproducing test to see 
exactly what would cause Question to be retuned as None.


-- 
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] update table record

2012-02-03 Thread Michael Bayer

On Feb 3, 2012, at 1:04 PM, Mason wrote:

 Hi,
 
 If i have a table named 'message' and it has the following columns:
 message_id, tar_del, src_del.  I pass in del_list which is a list of
 message to delete.  I tried the following
 
 self.session.query(Message)\
 .filter(Message.message_id.in_(del_list))\
 .update({Message.tar_del: self._DELETE})
 
 but kept getting this error
 
  File /usr/lib64/python2.7/site-packages/sqlalchemy/orm/query.py,
 line 2598, in update
Could not evaluate current criteria in Python. 
 InvalidRequestError: Could not evaluate current criteria in Python.
 Specify 'fetch' or False for the synchronize_session parameter.

the error message refers to the synchronize_session parameter of the update 
method:

http://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=query.update#sqlalchemy.orm.query.Query.update

pass False for this value instead of the default of evaluate:

query.update({Message.tar_del:self._DELETE}, synchronize_session=False)


-- 
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 a standalone SEQUENCE

2012-02-03 Thread Michael Bayer

On Feb 3, 2012, at 6:24 PM, Michael Hipp wrote:

 Is there an example or other explanation of how to use a SEQUENCE that is not 
 a primary key on a table, particularly how to create the sequence, get next 
 value, reset seq, etc. I see the base docs here, but it's not obvious to me 
 how exactly to use this class.
 
 http://docs.sqlalchemy.org/en/latest/core/schema.html#sqlalchemy.schema.Sequence

the sequence is created on the Python side given a name:

mysequence = Sequence(mysequence)

so the docstrings list out create(), used as such:

mysequence.create(engine)

emits CREATE SEQUENCE

and next_value(), returns a func element which can be executed:

engine.execute(mysequence.next_value())

there's not a built in feature for RESET or other alterations of sequences 
right now, you'd have to execute() the appropriate SQL for your target backend.


-- 
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.