[sqlalchemy] Re: Handling unique constraints
Matt Haggard wrote: I'm using SQLAlchemy with Pylons and am having trouble validating data. I have an App object mapped to a table with a unique constraint on App.number. Here's some code: q = Session.query(App) if app_id: q = q.filter_by(id=app_id).first() if q: c.app = q number = request.params.get('number') notes = request.params.get('notes') if appmodel and number: try: q.number = number q.notes = notes Session.save(q) Session.commit() c.message = 'Record updated' except: # restore pre-form data ?? how?? c.message = 'Error updating record' return render('index.mtl') else: return self.index() My questions are: 1) When I do the try statement, the value of q.number changes to whatever the user passed in via the form -- even if it's invalid, so that when I render the page, the invalid value is used. How do I reset the object to have the values it had before I did the try? Do I have to get it afresh from the db? try something like session.refresh( obj) or similar 2) How do I let the user know which value caused the record not to update? What information does SQLAlchemy provide back that I can use to say: You're number must be unique... and such-and-such must be greater than 0, etc..? mmh, do not mistake DB-constraints with validation-rules. the only sensible info u can get here is that the record is not unique (check what sort of exception that throws), but any further interpretation - why, what, where - is up to you - u have to know what field u have just set/changed, etc. --~--~-~--~~~---~--~~ 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: Emptying out the session of new objects
On Jan 4, 2008, at 9:46 PM, Dave Harrison wrote: Hey Mike, Below is a minimal test case that always produces the below failure for me under 0.4.2 but not under 0.4.1, OK, its actually something that was buggy in 0.4.1 but didnt produce a symptom, give r4003 a try which fixes this issue...0.4.2a is today --~--~-~--~~~---~--~~ 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] Schema display
Hi Guys, I was wondering where the function create_schema_graph has gone, or what it has changed to. Any assistance would be appreciated. Let me know, Morgan --~--~-~--~~~---~--~~ 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: Emptying out the session of new objects
On Sunday 06 January 2008 05:30:04 Michael Bayer wrote: On Jan 4, 2008, at 9:46 PM, Dave Harrison wrote: Hey Mike, Below is a minimal test case that always produces the below failure for me under 0.4.2 but not under 0.4.1, OK, its actually something that was buggy in 0.4.1 but didnt produce a symptom, give r4003 a try which fixes this issue...0.4.2a is today Hi Mike, That seems to have solved it for me. Thanks for the quick turn around, SQLAlchemy and the team continue to amaze ! :-) Cheers Dave --~--~-~--~~~---~--~~ 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] Polymorphic mappers on boolean types
Hey all, More fun with inheritance and mappers. In the following situation where the polymorphic type is a Boolean, the mapper for the object that matches on False incorrectly returns the parent object. Cheers Dave -- -- testapi.py -- from sqlalchemy import * from sqlalchemy.orm import * session = scoped_session( sessionmaker(autoflush=False, transactional=True) ) mapper = session.mapper metadata = MetaData() ANIMAL_TYPE_DOG = True ANIMAL_TYPE_CAT = False VET_TYPE_CITY = 1 VET_TYPE_COUNTRY = 2 animalTable = Table( 'animal', metadata, Column('id', Integer, primary_key=True), Column('type', Boolean, nullable=False), Column('name', String(100), nullable=False), Column('vet_id', Integer, ForeignKey('vet.id')), ) vetTable = Table( 'vet', metadata, Column('id', Integer, primary_key=True), Column('address', String(100), nullable=False), Column('kennel', Boolean, nullable=False), ) class _Animal(object): pass class Cat(_Animal): pass class Dog(_Animal): pass class Vet(object): pass vetMapper = mapper( Vet, vetTable, properties = { animals: relation( _Animal, backref=backref(vet, uselist=False), cascade=all, delete-orphan ), } ) animalMapper = mapper( _Animal, animalTable, polymorphic_on=animalTable.c.type, ) mapper( Dog, inherits=animalMapper, polymorphic_identity=ANIMAL_TYPE_DOG, ) mapper( Cat, inherits=animalMapper, polymorphic_identity=ANIMAL_TYPE_CAT, ) def connect(uri): engine = create_engine(uri, strategy=threadlocal) metadata.bind = engine return engine def create(): metadata.create_all() def drop(): metadata.drop_all() -- -- test.py -- import sys import testapi as db DB = sqlite:///memory db.connect(DB) db.create() v = db.Vet() v.address = 12 Foo St v.kennel = True c1 = db.Cat() c1.name = muffin c2 = db.Cat() c2.name = bagel d1 = db.Dog() d1.name = rex d2 = db.Dog() d2.name = bill db.session.flush() db.session.clear() for d in db.Query(db.Dog).all(): v.animals.append(d) for c in db.Query(db.Cat).all(): v.animals.append(c) db.session.flush() db.session.clear() v = db.Query(db.Vet).first() for a in v.animals: print a --~--~-~--~~~---~--~~ 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: Handling unique constraints
Matt, Take a look at the formencode module, it will simplify what you are trying to do here - handles all your parameter validation, form filling, and telling the user X,Y,Z are wrong etc. http://wiki.pylonshq.com/display/pylonsdocs/Form+Handling and formencode docs http://www.formencode.org/ On Jan 5, 12:13 am, [EMAIL PROTECTED] wrote: Matt Haggard wrote: I'm using SQLAlchemy with Pylons and am having trouble validating data. I have an App object mapped to a table with a unique constraint on App.number. Here's some code: q = Session.query(App) if app_id: q = q.filter_by(id=app_id).first() if q: c.app = q number = request.params.get('number') notes = request.params.get('notes') if appmodel and number: try: q.number = number q.notes = notes Session.save(q) Session.commit() c.message = 'Record updated' except: # restore pre-form data ?? how?? c.message = 'Error updating record' return render('index.mtl') else: return self.index() My questions are: 1) When I do the try statement, the value of q.number changes to whatever the user passed in via the form -- even if it's invalid, so that when I render the page, the invalid value is used. How do I reset the object to have the values it had before I did the try? Do I have to get it afresh from the db? try something like session.refresh( obj) or similar 2) How do I let the user know which value caused the record not to update? What information does SQLAlchemy provide back that I can use to say: You're number must be unique... and such-and-such must be greater than 0, etc..? mmh, do not mistake DB-constraints with validation-rules. the only sensible info u can get here is that the record is not unique (check what sort of exception that throws), but any further interpretation - why, what, where - is up to you - u have to know what field u have just set/changed, etc. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---