[sqlalchemy] Re: Column aliases

2007-01-19 Thread Jose Soares


I think I have a similar trouble, although the symptoms are different.
I have a PostgreSQL table named 'acl' mapped as UserGroup.

Table acl
   Column|  Type  
--+-

id   | integer
id_operatore | integer
cod_ruolo| text   
id_asl   | integer

id_azienda   | integer


I overriding some columns as: (take a look specially to group_id = 
cod_ruolo):


class UserGroup(DomainObject):
   pass
assign_mapper(context, UserGroup, tbl['acl'],
   properties = {  'user_id' : tbl['acl'].c.id_operatore,
   'id_operatore': tbl['acl'].c.id_operatore,
   'group_id': tbl['acl'].c.cod_ruolo,
   'cod_ruolo'   : tbl['acl'].c.cod_ruolo})

I have this problem, triyng to insert data...
the first line works but the second one, doesn't inser any data into 
column cod_ruolo.


1.   id_operatore = item.get('id_operatore'), group_id = 
item.get('cod_ruolo'))
2.   id_operatore = item.get('id_operatore'), cod_ruolo = 
item.get('cod_ruolo'))


Any ideas?
jo

Marco Mariani ha scritto:

Hi there

This relates to Turbogears, but is really a SA question.

I've customized TG authentication  authorization to use my autloaded
tables in Postgres and SqlAlchemy 0.3.3.

In my schema, I have User.c.uid, the login name of the users, as a
primary key

TG uses a User mapper with two distinct columns: User.c.user_id (the
primary key) and User.c.user_name (the logname).

Since I am an avid fan of meaningful primary keys (and have a legacy db
to support) I want to keep things my way, but TG does some user handling
that I have to fix.

So, to avoid patches to the TG source or useless sub-classing, I'd like
to access the same column by any of the three names.

I cannot do that with a python property on the mapper because TG should
be able to use get_by and friends.


I've come up with:


assign_mapper(context, User, tbl['users'], properties = {
'user_id': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
'user_name': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
'uid': tbl['users'].c.uid,
})



This seems to work (I added the third property to make 'uid' reappear!)
, but makes it impossible, for instance, to create new users:

In [1]: user = User(uid='xxx')

In [2]: session.flush()
[...]
SQLError: (IntegrityError) null value in column uid violates not-null
constraint
 'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
(%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None, 'name':
None}

In [3]: user = User(user_id='xxx')

In [4]: session.flush()
[...]
SQLError: (IntegrityError) null value in column uid violates not-null
constraint
 'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
(%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None, 'name':
None}



I reckon I should probably go ahead and patch TG, but maybe there is a
clean way to do what I have in mind?

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



[sqlalchemy] Re: Full Text Search using PostgreSQL and tsearch2

2007-01-19 Thread Sanjay


Thanks a lot, guys. Will try a sample application and post in the wiki.
Might take some time - meanwhile, if somebody gets some more stuff,
posting it would help.

thanks
sanjay


--~--~-~--~~~---~--~~
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: ORM ramblings 2 - and announcing some o2r wrapper

2007-01-19 Thread svilen

if you read closely, you can see that the embedded query for
selecting the employee is wrong; it has no FROM clause:

SELECT Employee.id AS id, Employee.name AS name,
Employee.atype AS atype, Employee.manager_id AS manager_id
\nWHERE Employee.atype = ?

SQLite is a little dumb in that it doesnt give a reasonable error
message for this condition (other DB's do).

when you see a SELECT that has no FROM, it usually means SA is
trying to correlate the select as a subquery to the enclosing
query.  this will occur anytime a select involving the employee
table occurs within another select involving the employee table. 
in this case its clearly wrong.


so the fix is just to insure that the subquery doesnt get
correlated:

ajoin = {
'Employee': employee_table.select( employee_table.c.atype
=='Employee', correlate=False),
'Manager': join( employee_table,
manager_table,manager_table.c.id ==employee_table.c.id),
}

and all is well again.
okay, would the join() also need .select(correlate=False) on it, or SA 
will not mistake it?


Next variant, if tables are concrete, it does same mistake - so i have 
to specify employee_table.select( correlate=False) instead of plain 
employee_table in the polymorphic_union. Anything else?


And, as i understand this applies only to things that go in a 
polymorphic_union; otherwise the correlate=False is not needed.




the polymorphic union thing is one of the most ambitious queries
SQLAlchemy's ORM produces.  which is why I still have not made it
Speaking of polyunion, i have made another version of your helper 
function. Your variant works ok if all tables under the poly-base  
are inherited in same way, i.e. either all are concrete_table, or all 
are table_inheritance.
My version (~5-6 lines changed) does not require this (see attached 
file), and works ok for any type of mixed inheritance.


btw, this correlate=False can be added automaticaly in the 
polyunion().
i've added it there and it seems to work... 
What do u think?


ciao
svil

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



polymunion.py
Description: application/python


[sqlalchemy] Using .label() on boolean expressions

2007-01-19 Thread King Simon-NFHD78


Hi,

I don't know if this is valid SQL, but MySQL seems to accept it... I'd
like to write a query that looks like:

 SELECT s.result LIKE 'Pass%' AS pass
 ...

Which would return 1 or 0 for each row depending on whether the result
column begins with Pass. In SQLAlchemy this would become:

 sa.select([s.c.result.startswith('Pass').label('pass')] ...)

Without the .label(), this works, but I can't label it because
BooleanExpressions don't have a label method.

Is there another way to do this?

Thanks,

Simon

--~--~-~--~~~---~--~~
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: Using .label() on boolean expressions

2007-01-19 Thread King Simon-NFHD78


Simon King wrote:


I don't know if this is valid SQL, but MySQL seems to accept 
it... I'd like to write a query that looks like:


  SELECT s.result LIKE 'Pass%' AS pass
  ...

Which would return 1 or 0 for each row depending on whether 
the result column begins with Pass.




Another way I tried to do this was to use the SQL:

SELECT IF(s.result LIKE 'Pass%', 'Pass', 'Fail') AS pass
...

because that would be a function rather than a boolean expression, and
functions can be labelled. I knew I couldn't call 'sa.func.if', but I
thought it would be nice if you could use sa.func.if_  - the
_FunctionGateway object could strip the trailing underscore from the
name. It took me a while to realise I could use sa.func.IF, but the
capital letters look ugly :-). Alternatively, _FunctionGateway could be
given a __call__ method which would take the name as a parameter, so you
could use 'sa.func(if)'.

Just an idea.

Simon

--~--~-~--~~~---~--~~
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: Column aliases

2007-01-19 Thread Michael Bayer


in both of these cases, a particular Column can only be expressed on  
the Mapper once.  I should add some error checking to Mapper to  
insure that this is followed.  The reason is simple:  if a class has  
two attributes a and b which both point to a column C, and I  
set a to 5 and b to 7 and then flush(), which attribute do I  
persist ?


therefore, a quick way to make a synonym for these attributes is to  
use synonym with proxy=True which will automatically set up the  
attribute on the class:


mapper(Class, table, properties={
'id':table.c.id,
'name':synonym('id', proxy=True)
})

where both id and name utlimately point to just the id  
information on the class.



On Jan 19, 2007, at 9:48 AM, Jose Soares wrote:



I think I have a similar trouble, although the symptoms are different.
I have a PostgreSQL table named 'acl' mapped as UserGroup.

Table acl
   Column|  Type  --+-
id   | integer
id_operatore | integer
cod_ruolo| text   id_asl   | integer
id_azienda   | integer


I overriding some columns as: (take a look specially to group_id =  
cod_ruolo):


class UserGroup(DomainObject):
   pass
assign_mapper(context, UserGroup, tbl['acl'],
   properties = {  'user_id' : tbl['acl'].c.id_operatore,
   'id_operatore': tbl['acl'].c.id_operatore,
   'group_id': tbl['acl'].c.cod_ruolo,
   'cod_ruolo'   : tbl['acl'].c.cod_ruolo})

I have this problem, triyng to insert data...
the first line works but the second one, doesn't inser any data  
into column cod_ruolo.


1.   id_operatore = item.get('id_operatore'), group_id = item.get 
('cod_ruolo'))
2.   id_operatore = item.get('id_operatore'), cod_ruolo = item.get 
('cod_ruolo'))


Any ideas?
jo

Marco Mariani ha scritto:

Hi there

This relates to Turbogears, but is really a SA question.

I've customized TG authentication  authorization to use my autloaded
tables in Postgres and SqlAlchemy 0.3.3.

In my schema, I have User.c.uid, the login name of the users, as a
primary key

TG uses a User mapper with two distinct columns: User.c.user_id (the
primary key) and User.c.user_name (the logname).

Since I am an avid fan of meaningful primary keys (and have a  
legacy db
to support) I want to keep things my way, but TG does some user  
handling

that I have to fix.

So, to avoid patches to the TG source or useless sub-classing, I'd  
like

to access the same column by any of the three names.

I cannot do that with a python property on the mapper because TG  
should

be able to use get_by and friends.


I've come up with:


assign_mapper(context, User, tbl['users'], properties = {
'user_id': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
'user_name': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
'uid': tbl['users'].c.uid,
})



This seems to work (I added the third property to make 'uid'  
reappear!)

, but makes it impossible, for instance, to create new users:

In [1]: user = User(uid='xxx')

In [2]: session.flush()
[...]
SQLError: (IntegrityError) null value in column uid violates not- 
null

constraint
 'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
(%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None,  
'name':

None}

In [3]: user = User(user_id='xxx')

In [4]: session.flush()
[...]
SQLError: (IntegrityError) null value in column uid violates not- 
null

constraint
 'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
(%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None,  
'name':

None}



I reckon I should probably go ahead and patch TG, but maybe there  
is a

clean way to do what I have in mind?

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



[sqlalchemy] Re: ORM ramblings 2 - and announcing some o2r wrapper

2007-01-19 Thread Michael Bayer


sum up for me what the current bugs youre observing are.  the  
foreignkey parameter is definitely needed for some of your cases.   
it un-ambiguates which column in the join condition is remote, for  
a join where its otherwise not clear.



On Jan 19, 2007, at 7:32 AM, svilen wrote:


and that change is in rev 2214, your two test scripts run
unmodified now.


without these changes (pure 0.3.3), adding explicit foreignkey=...  
on all relations worked OK in all 39 cases.


With new changes (trunk/2216), B pointing to A (manager pointing to  
employee) does not work. B pointing to B is ok, A pointing to A or  
B is ok.
sqlalchemy.exceptions.ArgumentError: Cant determine relation  
direction for 'manager' on mapper 'Mapper|Manager|Manager' with  
primary join 'Manager.manager_id = Employee.id' - foreign key  
columns are present in both the parent and the child's mapped  
tables.  Specify 'foreignkey' argument.


regardless that foreignkey= argument is there.

see attach.

--
Thanks for the other changes. Here's a fixed polumunion.py. It is  
best if typecolname there is always given - it will be used only if  
needed (concrete). But by default i've made it fit your present  
protocol.



test_case3.py
polymunion.py



--~--~-~--~~~---~--~~
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: Using .label() on boolean expressions

2007-01-19 Thread Michael Bayer


I just changed the inheritance of BooleanExpression to be from  
BinaryExpression so it retains all the semantics of a  
BinaryExpression, rev 2217.  this required a slight tweak in the  
orm's Query.select() method to properly detect a where criterion  
but was otherwise not a big deal.


On Jan 19, 2007, at 10:11 AM, King Simon-NFHD78 wrote:



Hi,

I don't know if this is valid SQL, but MySQL seems to accept it... I'd
like to write a query that looks like:

 SELECT s.result LIKE 'Pass%' AS pass
 ...

Which would return 1 or 0 for each row depending on whether the result
column begins with Pass. In SQLAlchemy this would become:

 sa.select([s.c.result.startswith('Pass').label('pass')] ...)

Without the .label(), this works, but I can't label it because
BooleanExpressions don't have a label method.

Is there another way to do this?

Thanks,

Simon





--~--~-~--~~~---~--~~
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: Using .label() on boolean expressions

2007-01-19 Thread Michael Bayer



On Jan 19, 2007, at 11:49 AM, King Simon-NFHD78 wrote:


because that would be a function rather than a boolean expression, and
functions can be labelled. I knew I couldn't call 'sa.func.if', but I
thought it would be nice if you could use sa.func.if_  - the
_FunctionGateway object could strip the trailing underscore from the
name.


good idea, i put that in r2218.





--~--~-~--~~~---~--~~
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: Column aliases

2007-01-19 Thread jose


I tried this

class UserGroup(DomainObject):
   pass
assign_mapper(context, UserGroup, tbl['acl'],
   properties = {
 
'id_operatore': tbl['acl'].c.id_operatore, 'user_id' : synonym('id_operatore', proxy=True),

'cod_ruolo'   : tbl['acl'].c.cod_ruolo,'group_id': 
synonym('cod_ruolo',proxy=True)

}
)
but...
 File build/bdist.linux-i686/egg/sqlalchemy/orm/mapper.py, line 552, in init
 File build/bdist.linux-i686/egg/sqlalchemy/ext/assignmapper.py, line 23, in 
__init__
 File build/bdist.linux-i686/egg/sqlalchemy/orm/properties.py, line 38, in 
__set__
AttributeError: 'SynonymProperty' object has no attribute 'set'


Michael Bayer wrote:



in both of these cases, a particular Column can only be expressed on  
the Mapper once.  I should add some error checking to Mapper to  
insure that this is followed.  The reason is simple:  if a class has  
two attributes a and b which both point to a column C, and I  
set a to 5 and b to 7 and then flush(), which attribute do I  
persist ?


therefore, a quick way to make a synonym for these attributes is to  
use synonym with proxy=True which will automatically set up the  
attribute on the class:


mapper(Class, table, properties={
'id':table.c.id,
'name':synonym('id', proxy=True)
})

where both id and name utlimately point to just the id  
information on the class.



On Jan 19, 2007, at 9:48 AM, Jose Soares wrote:



I think I have a similar trouble, although the symptoms are different.
I have a PostgreSQL table named 'acl' mapped as UserGroup.

Table acl
   Column|  Type  --+-
id   | integer
id_operatore | integer
cod_ruolo| text   id_asl   | integer
id_azienda   | integer


I overriding some columns as: (take a look specially to group_id =  
cod_ruolo):


class UserGroup(DomainObject):
   pass
assign_mapper(context, UserGroup, tbl['acl'],
   properties = {  'user_id' : tbl['acl'].c.id_operatore,
   'id_operatore': tbl['acl'].c.id_operatore,
   'group_id': tbl['acl'].c.cod_ruolo,
   'cod_ruolo'   : tbl['acl'].c.cod_ruolo})

I have this problem, triyng to insert data...
the first line works but the second one, doesn't inser any data  into 
column cod_ruolo.


1.   id_operatore = item.get('id_operatore'), group_id = item.get 
('cod_ruolo'))
2.   id_operatore = item.get('id_operatore'), cod_ruolo = item.get 
('cod_ruolo'))


Any ideas?
jo

Marco Mariani ha scritto:


Hi there

This relates to Turbogears, but is really a SA question.

I've customized TG authentication  authorization to use my autloaded
tables in Postgres and SqlAlchemy 0.3.3.

In my schema, I have User.c.uid, the login name of the users, as a
primary key

TG uses a User mapper with two distinct columns: User.c.user_id (the
primary key) and User.c.user_name (the logname).

Since I am an avid fan of meaningful primary keys (and have a  
legacy db
to support) I want to keep things my way, but TG does some user  
handling

that I have to fix.

So, to avoid patches to the TG source or useless sub-classing, I'd  
like

to access the same column by any of the three names.

I cannot do that with a python property on the mapper because TG  
should

be able to use get_by and friends.


I've come up with:


assign_mapper(context, User, tbl['users'], properties = {
'user_id': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
'user_name': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
'uid': tbl['users'].c.uid,
})



This seems to work (I added the third property to make 'uid'  
reappear!)

, but makes it impossible, for instance, to create new users:

In [1]: user = User(uid='xxx')

In [2]: session.flush()
[...]
SQLError: (IntegrityError) null value in column uid violates not- 
null

constraint
 'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
(%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None,  
'name':

None}

In [3]: user = User(user_id='xxx')

In [4]: session.flush()
[...]
SQLError: (IntegrityError) null value in column uid violates not- 
null

constraint
 'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
(%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None,  
'name':

None}



I reckon I should probably go ahead and patch TG, but maybe there  is a
clean way to do what I have in mind?

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



[sqlalchemy] Re: Column aliases

2007-01-19 Thread Michael Bayer

thanks for testing that for me.  added a fix + unit test in rev 2219.


On Jan 19, 2007, at 4:48 PM, jose wrote:


 I tried this

 class UserGroup(DomainObject):
pass
 assign_mapper(context, UserGroup, tbl['acl'],
properties = {
  'id_operatore': tbl 
 ['acl'].c.id_operatore, 'user_id' : synonym('id_operatore',  
 proxy=True),
 'cod_ruolo'   : tbl['acl'].c.cod_ruolo, 
 'group_id': synonym('cod_ruolo',proxy=True)

 }
 )
 but...
  File build/bdist.linux-i686/egg/sqlalchemy/orm/mapper.py, line  
 552, in init
  File build/bdist.linux-i686/egg/sqlalchemy/ext/assignmapper.py,  
 line 23, in __init__
  File build/bdist.linux-i686/egg/sqlalchemy/orm/properties.py,  
 line 38, in __set__
 AttributeError: 'SynonymProperty' object has no attribute 'set'


 Michael Bayer wrote:


 in both of these cases, a particular Column can only be expressed  
 on  the Mapper once.  I should add some error checking to Mapper  
 to  insure that this is followed.  The reason is simple:  if a  
 class has  two attributes a and b which both point to a column  
 C, and I  set a to 5 and b to 7 and then flush(), which  
 attribute do I  persist ?

 therefore, a quick way to make a synonym for these attributes is  
 to  use synonym with proxy=True which will automatically set up  
 the  attribute on the class:

 mapper(Class, table, properties={
 'id':table.c.id,
 'name':synonym('id', proxy=True)
 })

 where both id and name utlimately point to just the id   
 information on the class.


 On Jan 19, 2007, at 9:48 AM, Jose Soares wrote:


 I think I have a similar trouble, although the symptoms are  
 different.
 I have a PostgreSQL table named 'acl' mapped as UserGroup.

 Table acl
Column|  Type  --+-
 id   | integer
 id_operatore | integer
 cod_ruolo| text   id_asl   | integer
 id_azienda   | integer


 I overriding some columns as: (take a look specially to group_id  
 =  cod_ruolo):

 class UserGroup(DomainObject):
pass
 assign_mapper(context, UserGroup, tbl['acl'],
properties = {  'user_id' : tbl['acl'].c.id_operatore,
'id_operatore': tbl['acl'].c.id_operatore,
'group_id': tbl['acl'].c.cod_ruolo,
'cod_ruolo'   : tbl['acl'].c.cod_ruolo})

 I have this problem, triyng to insert data...
 the first line works but the second one, doesn't inser any data   
 into column cod_ruolo.

 1.   id_operatore = item.get('id_operatore'), group_id = item.get  
 ('cod_ruolo'))
 2.   id_operatore = item.get('id_operatore'), cod_ruolo =  
 item.get ('cod_ruolo'))

 Any ideas?
 jo

 Marco Mariani ha scritto:

 Hi there

 This relates to Turbogears, but is really a SA question.

 I've customized TG authentication  authorization to use my  
 autloaded
 tables in Postgres and SqlAlchemy 0.3.3.

 In my schema, I have User.c.uid, the login name of the users, as a
 primary key

 TG uses a User mapper with two distinct columns: User.c.user_id  
 (the
 primary key) and User.c.user_name (the logname).

 Since I am an avid fan of meaningful primary keys (and have a   
 legacy db
 to support) I want to keep things my way, but TG does some user   
 handling
 that I have to fix.

 So, to avoid patches to the TG source or useless sub-classing,  
 I'd  like
 to access the same column by any of the three names.

 I cannot do that with a python property on the mapper because  
 TG  should
 be able to use get_by and friends.


 I've come up with:


 assign_mapper(context, User, tbl['users'], properties = {
 'user_id': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
 'user_name': tbl['users'].c.uid,   # alias for  
 SqlAlchemyIdentity
 'uid': tbl['users'].c.uid,
 })



 This seems to work (I added the third property to make 'uid'   
 reappear!)
 , but makes it impossible, for instance, to create new users:

 In [1]: user = User(uid='xxx')

 In [2]: session.flush()
 [...]
 SQLError: (IntegrityError) null value in column uid violates  
 not- null
 constraint
  'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
 (%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid':  
 None,  'name':
 None}

 In [3]: user = User(user_id='xxx')

 In [4]: session.flush()
 [...]
 SQLError: (IntegrityError) null value in column uid violates  
 not- null
 constraint
  'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
 (%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid':  
 None,  'name':
 None}



 I reckon I should probably go ahead and patch TG, but maybe  
 there  is a
 clean way to do what I have in mind?

 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 [EMAIL PROTECTED]
For more options, visit 

[sqlalchemy] Re: how to find out the last insert id ?

2007-01-19 Thread jose

Michael Bayer wrote:

dischdennis wrote:
  

what is the easiest way to find out the last insert id? (MySQL 5)

supplast =
select([func.last_insert_id()],app_schema.SupplierTable.c.pr_supplier_ID


0).execute().fetchone()[0]
  

does not work for some reason


Dennis



result = table.insert().execute(**kwargs)

id = result.last_inserted_ids()[0]

# or, more mysql specific, but works with straight text SQL as well

id = result.cursor.lastrowid


I would like to do that using the mapper, but...
In a PostgreSQL DB I have two tables Anag and User linked by Anag.c.id 
== User.c.anag_id
Anag.c.id is a serial type, after inserted the Anag row, I want to find 
the last id to insert it into the User table.
I tried this code using the command 
select([func.max(Anag.c.id)]).execute().fetchone()[0]
to have the last id, but it doesn't returns the last id but the previous 
one.

   anag = Anag(
name= 'pippo',
address = 'rue cinc',
)
session.save(anag)
session.flush()
user = User(
anag_id = 
select([func.max(Anag.c.id)]).execute().fetchone()[0]
)

jo



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