Re: [sqlalchemy] twophase error sqlalchemy

2013-06-26 Thread Pau Tallada
Ok, that was my fault.

I was calling rollback() on a sqla.orm.session, instead of calling
transaction.abort()
Although the documentation only forbids calling commit, it does not say
anything about rolling back an individual session :(

Thanks!

Pau.

2013/6/25 Michael Bayer mike...@zzzcomputing.com

 looks like a zope.transaction issue to me, or at least one where they'd
 have to show me how they need to use the Session.  They seem to be calling
 upon session.transaction.prepare() directly at a time when that
 session.transaction is no longer associated with the parent session.
 You'd need to take it up with the zope.transaction folks first.




 On Jun 25, 2013, at 5:49 AM, Pau Tallada tall...@pic.es wrote:

 Hi!

 I think I've stepped on the same or similar bug.

 I have a software which uses zodb transactions to synchronize operations
 in two databases. In the end, the two databases are the same (we are using
 the exact same postgresql connection url for both). The software runs fine
 if only one instance is launched.

 But... if I launch more than one instance at a time, there is a great
 possibility that I get an Exception like this:

 Traceback (most recent call last):
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/brownthrower/dispatcher/static/__init__.py,
 line 238, in _run_job
 transaction.commit()
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/transaction/_manager.py,
 line 111, in commit
 return self.get().commit()
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/transaction/_transaction.py,
 line 280, in commit
 reraise(t, v, tb)
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/transaction/_transaction.py,
 line 271, in commit
 self._commitResources()
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/transaction/_transaction.py,
 line 417, in _commitResources
 reraise(t, v, tb)
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/transaction/_transaction.py,
 line 394, in _commitResources
 rm.tpc_vote(self)
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/zope/sqlalchemy/datamanager.py,
 line 153, in tpc_vote
 self.tx.prepare()
   File
 /nfs/pau/PAUdm/codes/tonello/ENV/packaging_test/lib/python2.6/site-packages/sqlalchemy/orm/session.py,
 line 313, in prepare
 if self._parent is not None or not self.session.twophase:
 AttributeError: 'NoneType' object has no attribute 'twophase'

 I'm completely lost on this :(

 Any clues?

 BTW, sessions are created like this:

 engine = create_engine('postgresql://foo')

 session_maker = scoped_session(sessionmaker(
 bind = engine,
 twophase = True,
 extension = ZopeTransactionExtension()
 ))

 Thank you very much in advance :)

 Pau.

 2013/2/20 Michael Bayer mike...@zzzcomputing.com

 you'd have to illustrate a working example of the code that causes that.


 On Feb 20, 2013, at 6:18 AM, Christian Démolis 
 christiandemo...@gmail.com wrote:

 Hi,

 AttributeError: 'NoneType' object has no attribute 'twophase'



 243.
 244.
 245.
 246.
 247.
 248.

 249.
 250.
 251.
 252.


 Session already has a Connection associated for the 
 
 given Connection's Engine)




 else:
 conn = bind.contextual_connect()




 if self.session.twophase and self._parent is None:




 transaction = conn.begin_twophase()
 elif self.nested:




 transaction = conn.begin_nested()
 else:


 Anybody understand this error ?

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.





 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.






 --
 --
 Pau Tallada Crespí
 Dep. d'Astrofísica i Cosmologia
 Port d'Informació Científica (PIC)
 Tel: +34 93 586 8233
 --


 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to 

Re: [sqlalchemy] HSTORE serialize/de-serialize incorrectly handles backslashes

2013-06-26 Thread Ryan Kelly
On Tue, Jun 06/25/13, 2013 at 02:47:18PM -0400, Michael Bayer wrote:
 
 On Jun 25, 2013, at 2:13 PM, Ryan Kelly rpkell...@gmail.com wrote:

  There are also some other parsing problems that I consider to be corner
  cases and broken as implemented in PostgreSQL, such as:
  
  (postgres@[local]:5432 14:05:43) [dev] select 'a=,b='::hstore;
  (postgres@[local]:5432 14:05:47) [dev] select 'a=, b='::hstore;
  (postgres@[local]:5432 14:06:45) [dev] select 'a= , b='::hstore;
  (postgres@[local]:5432 14:06:48) [dev] select 'a= ,b='::hstore;
  (postgres@[local]:5432 14:06:50) [dev] select 'a=,'::hstore;
  (postgres@[local]:5432 14:10:12) [dev] select ',=,'::hstore;
  
  None of which are parsed by SQLAlchemy but some of which are parsed by
  PostgreSQL.
 
 Posgresql or psycopg2 ?   isn't the serialization here normally done 
 natively if you're on a more recent psycopg2?

Well these are really de-serialization. But yes, serialization and
de-serialization are handled natively by psycopg2. In some circumstances
we actually need to parse/write the values directly ourselves (usually
when reading/writing files in the COPY format). Unfortunately,
psycopg2's parser is much stricter than the documented format as it
requires quoted keys (which in some ways make sense, as the server will
never return anything unquoted) so none of the above examples work.

-Ryan P. Kelly

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




[sqlalchemy] Unable to store datetime.datetime.max using SQLAlchemy==0.8.1 with the mysql-python==1.2.4 driver

2013-06-26 Thread Pelle Almquist
Hi,
not sure if I should post this issue here or if its a mysql-python one but 
perhaps someone with better knowledge can help me figure that out?
A more pretty print version of this issue is available 
here: 
http://stackoverflow.com/questions/17315422/unable-to-store-datetime-datetime-max-using-sqlalchemy-0-8-1-with-the-mysql-pyt

I've noticed a change in behavior for storing datetime.datetime.max via 
`SQLAlchemy==SQLAlchemy==0.8.1` and going from `mysql-python==1.2.3` to 
`mysql-python==1.2.4`. By only changing the driver from 1.2.3 to 1.2.4 I go 
from being able to store to being unable to store it.

Where do I turn to for help in this matter? SQLAlchemy or mysql-python? Is 
this expected behaviour or a bug or do I have a bad setup? I fear that a 
change like this will break a lot of systems out there.

This is my SQLAlchemy setup:

from sqlalchemy import create_engine, Integer, DateTime, Column
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime

engine = create_engine('mysql://root@localhost/test_database', 
echo=True)
Base = declarative_base()

class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
age = Column(DateTime, default=datetime.max)

Base.metadata.create_all(engine)
session = sessionmaker(bind=engine)()
u = User()
session.add(u)
session.commit()

I also have a virtualenv called test. This is what happens when I run the 
code above.

(test)➜  ~  pip install MySQL-python==1.2.3
(test)➜  ~  python test.py
2013-06-26 10:29:18,885 INFO sqlalchemy.engine.base.Engine SELECT 
DATABASE()
2013-06-26 10:29:18,885 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:29:18,887 INFO sqlalchemy.engine.base.Engine SHOW 
VARIABLES LIKE 'character_set%%'
2013-06-26 10:29:18,887 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:29:18,891 INFO sqlalchemy.engine.base.Engine SHOW 
VARIABLES LIKE 'sql_mode'
2013-06-26 10:29:18,891 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:29:18,896 INFO sqlalchemy.engine.base.Engine DESCRIBE 
`users`
2013-06-26 10:29:18,896 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:29:18,904 INFO sqlalchemy.engine.base.Engine BEGIN 
(implicit)
2013-06-26 10:29:18,905 INFO sqlalchemy.engine.base.Engine INSERT INTO 
users (age) VALUES (%s)
2013-06-26 10:29:18,905 INFO sqlalchemy.engine.base.Engine 
(datetime.datetime(, 12, 31, 23, 59, 59, 99),)
2013-06-26 10:29:18,908 INFO sqlalchemy.engine.base.Engine COMMIT

And the database (test_database) looks like this:

mysql select * from users;
++-+
| id | age |
++-+
|  1 | -12-31 23:59:59 |
++-+
1 row in set (0.00 sec)

This is my expected result so nothing strange here.

However, by simply switching the driver to mysql-python==1.2.4 I get this 
result.

(test)➜  ~  pip install MySQL-python==1.2.4
(test)➜  ~  python test.py
2013-06-26 10:33:39,544 INFO sqlalchemy.engine.base.Engine SELECT 
DATABASE()
2013-06-26 10:33:39,544 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:33:39,546 INFO sqlalchemy.engine.base.Engine SHOW 
VARIABLES LIKE 'character_set%%'
2013-06-26 10:33:39,546 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:33:39,546 INFO sqlalchemy.engine.base.Engine SHOW 
VARIABLES LIKE 'sql_mode'
2013-06-26 10:33:39,546 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:33:39,547 INFO sqlalchemy.engine.base.Engine DESCRIBE 
`users`
2013-06-26 10:33:39,547 INFO sqlalchemy.engine.base.Engine ()
2013-06-26 10:33:39,551 INFO sqlalchemy.engine.base.Engine BEGIN 
(implicit)
2013-06-26 10:33:39,552 INFO sqlalchemy.engine.base.Engine INSERT INTO 
users (age) VALUES (%s)
2013-06-26 10:33:39,552 INFO sqlalchemy.engine.base.Engine 
(datetime.datetime(, 12, 31, 23, 59, 59, 99),)

/Users/pelle/.virtualenvs/test/lib/python2.7/site-packages/sqlalchemy/engine/default.py:324:
 
Warning: Datetime function: datetime field overflow
  cursor.execute(statement, parameters)

/Users/pelle/.virtualenvs/test/lib/python2.7/site-packages/sqlalchemy/engine/default.py:324:
 
Warning: Out of range value for column 'age' at row 1
  cursor.execute(statement, parameters)
2013-06-26 10:33:39,553 INFO sqlalchemy.engine.base.Engine COMMIT

And the database looks like this.

mysql select * from users;
++-+
| id | age |
++-+
|  1 | -00-00 00:00:00 |
++-+
1 row in set (0.00 sec)

So now all of the sudden I receive a warning `Warning: Datetime function: 
datetime field overflow` and I end up with a nullable value in my database.


-- 
You received this message because you are subscribed to the Google Groups 

Re: [sqlalchemy] HSTORE serialize/de-serialize incorrectly handles backslashes

2013-06-26 Thread Michael Bayer

On Jun 26, 2013, at 6:57 AM, Ryan Kelly rpkell...@gmail.com wrote:

 On Tue, Jun 06/25/13, 2013 at 02:47:18PM -0400, Michael Bayer wrote:
 
 On Jun 25, 2013, at 2:13 PM, Ryan Kelly rpkell...@gmail.com wrote:
 
 There are also some other parsing problems that I consider to be corner
 cases and broken as implemented in PostgreSQL, such as:
 
 (postgres@[local]:5432 14:05:43) [dev] select 'a=,b='::hstore;
 (postgres@[local]:5432 14:05:47) [dev] select 'a=, b='::hstore;
 (postgres@[local]:5432 14:06:45) [dev] select 'a= , b='::hstore;
 (postgres@[local]:5432 14:06:48) [dev] select 'a= ,b='::hstore;
 (postgres@[local]:5432 14:06:50) [dev] select 'a=,'::hstore;
 (postgres@[local]:5432 14:10:12) [dev] select ',=,'::hstore;
 
 None of which are parsed by SQLAlchemy but some of which are parsed by
 PostgreSQL.
 
 Posgresql or psycopg2 ?   isn't the serialization here normally done 
 natively if you're on a more recent psycopg2?
 
 Well these are really de-serialization. But yes, serialization and
 de-serialization are handled natively by psycopg2. In some circumstances
 we actually need to parse/write the values directly ourselves (usually
 when reading/writing files in the COPY format). Unfortunately,
 psycopg2's parser is much stricter than the documented format as it
 requires quoted keys (which in some ways make sense, as the server will
 never return anything unquoted) so none of the above examples work.


OK just trying to get my head around the issue - psycopg2's parser does not 
have the backslashing issue right ?





 
 -Ryan P. Kelly
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Unable to store datetime.datetime.max using SQLAlchemy==0.8.1 with the mysql-python==1.2.4 driver

2013-06-26 Thread Michael Bayer

On Jun 26, 2013, at 10:18 AM, Pelle Almquist pe...@wrapp.com wrote:

 Hi,
 not sure if I should post this issue here or if its a mysql-python one but 
 perhaps someone with better knowledge can help me figure that out?
 A more pretty print version of this issue is available here: 
 http://stackoverflow.com/questions/17315422/unable-to-store-datetime-datetime-max-using-sqlalchemy-0-8-1-with-the-mysql-pyt
 
 I've noticed a change in behavior for storing datetime.datetime.max via 
 `SQLAlchemy==SQLAlchemy==0.8.1` and going from `mysql-python==1.2.3` to 
 `mysql-python==1.2.4`. By only changing the driver from 1.2.3 to 1.2.4 I go 
 from being able to store to being unable to store it.
 
 Where do I turn to for help in this matter? SQLAlchemy or mysql-python? Is 
 this expected behaviour or a bug or do I have a bad setup? I fear that a 
 change like this will break a lot of systems out there.

Well it seems like MySQL-python doesn't like trying to store a date with the 
year  here, it probably changed something about how it renders dates.  So 
yeah you'd need to take it up with Python-MySQL.

I would note that there are some good alternatives to MySQL-Python on the scene 
now, there's OurSQL, PyMySQL, and MySQL-connector-python seems to be working 
decently now, in case that helps.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




[sqlalchemy] Declarative Sequences

2013-06-26 Thread Mat Mathews
Apologies if I'm posting a question to this group incorrectly or with bad 
etiquette. I've been reading the emails from this group for a few years and now 
have a question myself. 

I am automating the creation of our models and business objects by reading a 
schema definition from YAML. We use the declarative base, some metaclasses and 
superclasses, a couple of mixes.. and ultimately PostgreSQL 9.x (exclusively)

I create a custom type using a metaclass that creates the primary key column 
along with anything else, and decorate anything that needs `@declared_attr` 
appropriately, for the declarative mechanism to discover it. 

Long story short,

I would like to create custom sequences using Sequence() with a start value, 
using a declared Column, and not having to construct a Table itself, as 
described in the docs here 
http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html

I would like to do something like this:

class User(object):
id = Column(Integer, Sequence('user_id_seq', start=1), 
primary_key=True)

This does work, and emits the CREATE SEQUENCE, but does not set the owned table 
or the column to user.id

I get around this by using the default SERIAL, using DDL events, by updating 
the normal ALTER SEQUENCE [user_id_seq]  RESTART WITH [value]. 

However, I may have just missed something or misunderstood something deep in 
declarative.

My goal is to automate as much of the schema, model, and business object 
creation as possible using our DSL, which is leveraging the power of 
SQLAlchemy, GeoAlchemy2, PostgreSQL, and PostGIS.

Much obliged for any help,

Mat






-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Declarative Sequences

2013-06-26 Thread Michael Bayer

On Jun 26, 2013, at 12:02 PM, Mat Mathews m...@miga.me wrote:

 I would like to do something like this:
 
 class User(object):
   id = Column(Integer, Sequence('user_id_seq', start=1), 
 primary_key=True)
 
 This does work, and emits the CREATE SEQUENCE, but does not set the owned 
 table or the column to user.id

I'm not able to reproduce, even assigning the same sequence name to two 
different tables simultaneously produces the correct result.  Can you modify 
the test below to illustrate your issue?

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base, declared_attr

Base = declarative_base()

class IdMixin(object):
id = Column(Integer, Sequence('user_id_seq', start=1), primary_key=True)

class A(IdMixin, Base):
__tablename__ = 'a'

class B(IdMixin, Base):
__tablename__ = 'b'

e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
Base.metadata.drop_all(e)
Base.metadata.create_all(e)

sess = Session(e)

a1 = A()
sess.add(a1)

b1 = B()
sess.add(b1)
sess.commit()

assert a1.id == 1
assert b1.id == 10001


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Declarative Sequences

2013-06-26 Thread Mat Mathews
Thanks for the quick reply.

If I modify the name of the sequence in the test, it reproduces what I have 
experienced. 

When I check the details on the sequence in postgres, there is no table owner 
or column specified by the sequence 'test_user_id_seq'.. and I would expect to 
see both tables `a` and `b`.

class IdMixin(object):
   id = Column(Integer, Sequence('test_user_id_seq', start=1), 
primary_key=True)

I will write a complete self contained test, that anyone can run. But wanted to 
give my immediate feedback.

Thanks so much,
Mat


On Jun 26, 2013, at 6:37 PM, Michael Bayer mike...@zzzcomputing.com wrote:

 
 On Jun 26, 2013, at 12:02 PM, Mat Mathews m...@miga.me wrote:
 
 I would like to do something like this:
 
 class User(object):
  id = Column(Integer, Sequence('user_id_seq', start=1), 
 primary_key=True)
 
 This does work, and emits the CREATE SEQUENCE, but does not set the owned 
 table or the column to user.id
 
 I'm not able to reproduce, even assigning the same sequence name to two 
 different tables simultaneously produces the correct result.  Can you modify 
 the test below to illustrate your issue?
 
 from sqlalchemy import *
 from sqlalchemy.orm import *
 from sqlalchemy.ext.declarative import declarative_base, declared_attr
 
 Base = declarative_base()
 
 class IdMixin(object):
id = Column(Integer, Sequence('user_id_seq', start=1), 
 primary_key=True)
 
 class A(IdMixin, Base):
__tablename__ = 'a'
 
 class B(IdMixin, Base):
__tablename__ = 'b'
 
 e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
 Base.metadata.drop_all(e)
 Base.metadata.create_all(e)
 
 sess = Session(e)
 
 a1 = A()
 sess.add(a1)
 
 b1 = B()
 sess.add(b1)
 sess.commit()
 
 assert a1.id == 1
 assert b1.id == 10001
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Declarative Sequences

2013-06-26 Thread Michael Bayer

On Jun 26, 2013, at 1:26 PM, Mat Mathews m...@miga.me wrote:

 Thanks for the quick reply.
 
 If I modify the name of the sequence in the test, it reproduces what I have 
 experienced. 
 
 When I check the details on the sequence in postgres, there is no table owner 
 or column specified by the sequence 'test_user_id_seq'.. and I would expect 
 to see both tables `a` and `b`.
 
 class IdMixin(object):
   id = Column(Integer, Sequence('test_user_id_seq', start=1), 
 primary_key=True)
 
 I will write a complete self contained test, that anyone can run. But wanted 
 to give my immediate feedback.

oh, you're looking for a linkage in PG's information schema I guess?  
Sequence() doesn't have that functionality.   My understanding was that PG's 
SERIAL created the linkage as a server default, so you can get this by adding 
this server default yourself, see below for demo:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base, declared_attr

Base = declarative_base()

class IdMixin(object):
id = Column(Integer,
Sequence('some_id_seq', start=1),
server_default=text(nextval('some_id_seq')),
primary_key=True)

class A(IdMixin, Base):
__tablename__ = 'a'

class B(IdMixin, Base):
__tablename__ = 'b'

e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
Base.metadata.drop_all(e)
Base.metadata.create_all(e)

sess = Session(e)

a1 = A()
sess.add(a1)

b1 = B()
sess.add(b1)
sess.commit()

assert a1.id == 1
assert b1.id == 10001

sess.execute(INSERT INTO b DEFAULT VALUES)
assert sess.execute(SELECT * FROM b WHERE id=10002).scalar()




 
 Thanks so much,
 Mat
 
 
 On Jun 26, 2013, at 6:37 PM, Michael Bayer mike...@zzzcomputing.com wrote:
 
 
 On Jun 26, 2013, at 12:02 PM, Mat Mathews m...@miga.me wrote:
 
 I would like to do something like this:
 
 class User(object):
 id = Column(Integer, Sequence('user_id_seq', start=1), 
 primary_key=True)
 
 This does work, and emits the CREATE SEQUENCE, but does not set the owned 
 table or the column to user.id
 
 I'm not able to reproduce, even assigning the same sequence name to two 
 different tables simultaneously produces the correct result.  Can you modify 
 the test below to illustrate your issue?
 
 from sqlalchemy import *
 from sqlalchemy.orm import *
 from sqlalchemy.ext.declarative import declarative_base, declared_attr
 
 Base = declarative_base()
 
 class IdMixin(object):
   id = Column(Integer, Sequence('user_id_seq', start=1), 
 primary_key=True)
 
 class A(IdMixin, Base):
   __tablename__ = 'a'
 
 class B(IdMixin, Base):
   __tablename__ = 'b'
 
 e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
 Base.metadata.drop_all(e)
 Base.metadata.create_all(e)
 
 sess = Session(e)
 
 a1 = A()
 sess.add(a1)
 
 b1 = B()
 sess.add(b1)
 sess.commit()
 
 assert a1.id == 1
 assert b1.id == 10001
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Declarative Sequences

2013-06-26 Thread Mat Mathews
That absolutely solves my issue. I had a serious suspicion I was missing 
something, and it was to look at the Column options, not just the Sequence. 

Thanks a ton.

On Jun 26, 2013, at 7:44 PM, Michael Bayer mike...@zzzcomputing.com wrote:

 
 On Jun 26, 2013, at 1:26 PM, Mat Mathews m...@miga.me wrote:
 
 Thanks for the quick reply.
 
 If I modify the name of the sequence in the test, it reproduces what I have 
 experienced. 
 
 When I check the details on the sequence in postgres, there is no table 
 owner or column specified by the sequence 'test_user_id_seq'.. and I would 
 expect to see both tables `a` and `b`.
 
 class IdMixin(object):
  id = Column(Integer, Sequence('test_user_id_seq', start=1), 
 primary_key=True)
 
 I will write a complete self contained test, that anyone can run. But wanted 
 to give my immediate feedback.
 
 oh, you're looking for a linkage in PG's information schema I guess?  
 Sequence() doesn't have that functionality.   My understanding was that PG's 
 SERIAL created the linkage as a server default, so you can get this by 
 adding this server default yourself, see below for demo:
 
 from sqlalchemy import *
 from sqlalchemy.orm import *
 from sqlalchemy.ext.declarative import declarative_base, declared_attr
 
 Base = declarative_base()
 
 class IdMixin(object):
id = Column(Integer,
Sequence('some_id_seq', start=1),
server_default=text(nextval('some_id_seq')),
primary_key=True)
 
 class A(IdMixin, Base):
__tablename__ = 'a'
 
 class B(IdMixin, Base):
__tablename__ = 'b'
 
 e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
 Base.metadata.drop_all(e)
 Base.metadata.create_all(e)
 
 sess = Session(e)
 
 a1 = A()
 sess.add(a1)
 
 b1 = B()
 sess.add(b1)
 sess.commit()
 
 assert a1.id == 1
 assert b1.id == 10001
 
 sess.execute(INSERT INTO b DEFAULT VALUES)
 assert sess.execute(SELECT * FROM b WHERE id=10002).scalar()
 
 
 
 
 
 Thanks so much,
 Mat
 
 
 On Jun 26, 2013, at 6:37 PM, Michael Bayer mike...@zzzcomputing.com wrote:
 
 
 On Jun 26, 2013, at 12:02 PM, Mat Mathews m...@miga.me wrote:
 
 I would like to do something like this:
 
 class User(object):
id = Column(Integer, Sequence('user_id_seq', start=1), 
 primary_key=True)
 
 This does work, and emits the CREATE SEQUENCE, but does not set the owned 
 table or the column to user.id
 
 I'm not able to reproduce, even assigning the same sequence name to two 
 different tables simultaneously produces the correct result.  Can you 
 modify the test below to illustrate your issue?
 
 from sqlalchemy import *
 from sqlalchemy.orm import *
 from sqlalchemy.ext.declarative import declarative_base, declared_attr
 
 Base = declarative_base()
 
 class IdMixin(object):
  id = Column(Integer, Sequence('user_id_seq', start=1), 
 primary_key=True)
 
 class A(IdMixin, Base):
  __tablename__ = 'a'
 
 class B(IdMixin, Base):
  __tablename__ = 'b'
 
 e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
 Base.metadata.drop_all(e)
 Base.metadata.create_all(e)
 
 sess = Session(e)
 
 a1 = A()
 sess.add(a1)
 
 b1 = B()
 sess.add(b1)
 sess.commit()
 
 assert a1.id == 1
 assert b1.id == 10001
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.