[sqlalchemy] Re: default values in reflected tables

2009-07-27 Thread Jamie Bullock


On 27 Jul 2009, at 16:32, Michael Bayer wrote:
>
> what's "elegant" to me is:
>
>>>> f = Foo()
>>>> Session.add(f)
>>>> Session.flush() # or commit
>>>> f.bar
> True
>
> consider that Foo() wouldn't know about server generated defaults if  
> not
> associated with the database.  The database association is achieved  
> using
> Session.add().
>


Ah thanks, that works great and makes perfect sense.

Only issue now is that if you have a NOT NULL constraint on a field,  
an IntegrityError exception gets raised when flush() is called. One  
way round this is to supply a dummy value for the field in question.  
Is there a better way to prevent the exception?

Jamie

--~--~-~--~~~---~--~~
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] default values in reflected tables

2009-07-27 Thread Jamie Bullock


Hi,

Is there a way in SQLalchemy to have the values of a mapped reflected
table object set to default column values derived from the database
backend?

e.g.  if column 'bar' of table 'foo' in my database has a default of
"True", I would like to be able to do the following:

>>> foo_table = Table("foo", metadata, autoload=True, autoload_with=engine)
>>> mapper(Foo, foo_table)
>>> f = Foo()
>>> f.bar
True

Instead f.bar is None.

Is there an elegant way to achieve this without querying
information_schema and setting the defaults from there?

Jamie

--~--~-~--~~~---~--~~
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: Column defaults not being created with tables.

2007-01-05 Thread Jamie


Michael,

Thanks for the reply (and the help).


--~--~-~--~~~---~--~~
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] Column defaults not being created with tables.

2007-01-05 Thread Jamie


I'm a SQLAlchemy newbie and sure I'm doing something simply stupidly
wrong, but the column defaults are being ignored when I create a table.
Does anyone have any suggestions? Thank you!

Python 2.4.3
SQLAlchemy 0.3.3
MySQLdb 1.2.1

#
from sqlalchemy import *

db = create_engine('mysql://username:[EMAIL PROTECTED]/testdb')
meta = BoundMetaData(db)

users = Table('users', meta,
   Column('user_id', Integer, primary_key=True),
   Column('username', String(32), nullable=False),
   Column('email_address', String(60), unique=True, nullable=False),
   Column('password', String(20), nullable=False),
   Column('created', DateTime, default=func.current_timestamp(),
nullable=False)
)
# default=func.now() is also ignored.

meta.engine.echo = True
meta.create_all()

#
2007-01-05 12:16:02,372 INFO sqlalchemy.engine.base.Engine.0x..10 show
table status like 'users'
2007-01-05 12:16:02,373 INFO sqlalchemy.engine.base.Engine.0x..10 None
2007-01-05 12:16:02,376 INFO sqlalchemy.engine.base.Engine.0x..10 show
table status like 'users'
2007-01-05 12:16:02,376 INFO sqlalchemy.engine.base.Engine.0x..10 None
2007-01-05 12:16:02,379 INFO sqlalchemy.engine.base.Engine.0x..10
CREATE TABLE users (
   user_id INTEGER NOT NULL AUTO_INCREMENT,
   username VARCHAR(32) NOT NULL,
   email_address VARCHAR(60) NOT NULL,
   password VARCHAR(20) NOT NULL,
   created DATETIME NOT NULL,
   PRIMARY KEY (user_id),
UNIQUE (email_address)
)


2007-01-05 12:16:02,379 INFO sqlalchemy.engine.base.Engine.0x..10 None
2007-01-05 12:16:02,394 INFO sqlalchemy.engine.base.Engine.0x..10 COMMIT


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