sqlalchemy beginner

Discussion in 'Python' started by Jabba Laci, Nov 21, 2011.

  1. Jabba Laci

    Jabba Laci Guest

    Hi,

    I'm reading the Essential SQLAlchemy book from O'Reilly. It explains
    SqlAlch 0.4 but my current version is 0.7 and there are some
    differences.

    Here is an example from the book:

    user_table = Table('tf_user', metadata,
    Column('id', Integer, primary_key=True),
    Column('user_name', Unicode(16), unique=True, nullable=False),
    Column('password', Unicode(40), nullable=False),
    Column('display_name', Unicode(255), default=''),
    Column('created', DateTime, default=datetime.now)
    )

    Here I get the following warning:

    SAWarning: Unicode column received non-unicode default value.
    Column('display_name', Unicode(255), default=''),

    Changing Unicode(255) to String(255) makes the warning disappear but
    I'm not sure if it's the correct solution.

    For table names, the book uses the prefix convention 'tf_' but what
    does it mean? 't' is table, but what is 'f'?

    Thanks,

    Laszlo
     
    Jabba Laci, Nov 21, 2011
    #1
    1. Advertisements

  2. Jabba Laci

    John Gordon Guest

    Perhaps it would help to supply the default value as a Unicode string
    instead of a plain string?

    Column('display_name', Unicode(255), default=u''),
     
    John Gordon, Nov 21, 2011
    #2
    1. Advertisements

  3. Jabba Laci

    Roy Smith Guest

    Everytime I've worked with SQLAlchemy, I've run away screaming in the
    other direction. Sure, portability is a good thing, but at what cost?
     
    Roy Smith, Nov 22, 2011
    #3
  4. Jabba Laci

    alex23 Guest

    I've never found SQLAlchemy to be anything but sane and approachable.
    It's really worth understanding _how_ it works so you can see there's
    no magic happening there.

    What cost do you see inherit in the use of SQLAlchemy?
     
    alex23, Nov 22, 2011
    #4
  5. Jabba Laci

    Roy Smith Guest

    The cost of understanding how it works :)

    Seriously. I understand SQL. Well, I'm not a SQL wizard, but I
    understand enough to do what I need to do. Whenever I have to use
    SQLAlchemy, I always find myself knowing exactly what SQL I want to
    write and scratching my head to figure out how to translate that into
    SQLAlchemy calls.
     
    Roy Smith, Nov 22, 2011
    #5
  6. | In article
    | <>,
    | > > Everytime I've worked with SQLAlchemy, I've run away screaming in the
    | > > other direction. ?Sure, portability is a good thing, but at what cost?
    | >
    | > I've never found SQLAlchemy to be anything but sane and approachable.
    | > It's really worth understanding _how_ it works so you can see there's
    | > no magic happening there.
    | >
    | > What cost do you see inherit in the use of SQLAlchemy?
    |
    | The cost of understanding how it works :)
    |
    | Seriously. I understand SQL. Well, I'm not a SQL wizard, but I
    | understand enough to do what I need to do. Whenever I have to use
    | SQLAlchemy, I always find myself knowing exactly what SQL I want to
    | write and scratching my head to figure out how to translate that into
    | SQLAlchemy calls.

    Are you trying to go the ORM route (make classes etc mapping to SQL
    entities etc)?

    I ask because I avoid ORM and mostly use the SQL syntax notation, eg:

    for node_id, attr, value in select( [ attrs.c.NODE_ID,
    attrs.c.ATTR,
    attrs.c.VALUE,
    ] ) \
    .order_by(asc(attrs.c.NODE_ID)) \
    .execute():

    or:

    self.attrs.delete(self.attrs.c.NODE_ID == node_id).execute()

    which I find very easy to read (self.attrs is an SQLAchemy table).

    ORMs seem very cool and all, but I personally prefer to work with simple
    SQL level schemae and python-level objects and classes i.e. not ORM
    classes but ordinary classes that cll SQLAlchemy
    select/update/delete/etc methods to manipulate the db.

    For me the great strengths of SQLA are that it (1) talks to many
    different backend DBs and (2) removes the need to write tediously quotes
    SQL because I can write python expressions like the above that map
    directly to SQL statements, and SQLA does all the quoting, conversion
    etc. Reliably!

    Cheers,
    --
    Cameron Simpson <> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    I sometimes wish that people would put a little more emphasis upon the
    observance of the law than they do upon its enforcement. - Calvin Coolidge
     
    Cameron Simpson, Nov 22, 2011
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.