Friday, May 23rd, 2008


I ♥ SQL Alchemy.

I’m currently rewriting a database so that the schema definition is in SQL Alchemy, allowing for us to deploy it across a range of platforms with a bit more ease.

We had been using some sequence types to automatically set the primary key to a new unique integer. Think Autoincrement in Access, if that’s where you’ve done some database stuff, or in MySQL I believe it’s AUTO_INCREMENT.

In SQL Alchemy, you get to define Tables in python, like this:


log_table = Table(‘log’, metadata,
Column(‘IP’, String(15), primary_key=True),
Column(‘timestamp’, DateTime, primary_key=True),
Column(‘reqSize’, Integer),
Column(‘resSize’, Integer),
Column(‘time’, Float),
Column(‘reqName’, String(256)),
Column(‘reqData’, String(4096)),
Column(‘resData’, String(4096)),
Column(‘reqObj’, String(4096)),
Column(‘resObj’, String(4096)))

Now, if you need a sequence type, then you can use a Sequence() object. But what if you have existing data, which may or may not have holes, and you need to ensure you don’t have any collisions?

db.query(Person).order_by('id').all()[-1].id + 1

This will query and get the Person object with the highest id, and add one to it.

You can then use this as an argument to the Sequence() object, and it will only generate the sequence from that value onwards.

View Comments (0)   RSS Feed for Comments on this Post

(While I wait for something to compile…)

Had to fill the car up with petrol this morning. Petrol prices are a touch under AU$1.60/litre, which is higher than it has ever been in Australia. It costs me about 3 hours of work to fill my car up.

Anyway, there was a bit of a line up at the servo, and another guy came out to serve. The woman in front of me did the shit that she wasn’t being served first (people behind me rushed over to his till), and swore at everyone and stormed out, leaving a newspaper and some chocolate bars on the counter. Did I mention she was a bit fat? And feral?

So, she stormed out, straight into her car, which she had apparently also filled up with petrol, and drove off.

The chick behind the counter didn’t really get her number properly, so I corrected her.

And scored myself a free coffee for it.

But it was worth if for the show alone.

View Comments (0)   RSS Feed for Comments on this Post