Excerpts from Mike Toews's message of Tue Dec 01 17:36:40 -0300 2009:
> Hi,
>
> I'm new to Pylons, and I would like to use it with an existing
> PostgreSQL database. I understand that SQLAlchemy can set up a
> complete database (using "metadata.create_all()"), complete with
> foreign keys and sequences, but I have absolutely no interest in this,
> since I have already crafted the database using conventional tools,
> and Pylons would be simply one of many client apps.
>
> So, where do I start to use my existing database with Pylons? I
> understand I need something like a model.py file, as explained in
> Chapter 7 of the Pylons Book. Do I have to reverse engineer the
> tables, sequences, field names/types/constraints, and relations? This
> sounds awful. Are there any tools to help me import or translate a
> database schema to a Python/SQLAlchemy model? With the Python model,
> can I choose to only define the names and types, and not define
> defaults and constraints (since they are already done within the
> database)?
>
> Thanks.
>
> -Mike
>
I have this application running on top of pgsql and I was asked to build
a reporting system for it.
When you create your pylons application and selected SqlAlchemy, you
will find the __init__.py in the /model dir with enough template code
to help you out with this: reflection is a great SqlAlchemy feature.
Just to guide you a little bit let me show two tables I have.
As you will see, I defined the mapper relation between an "users" table
and an User class (same for ticket - Ticket). You will notice I didn't
defined the tables or anything like that (I did build some relations due
to my requirements).
When you're done just start the server and you will have access to all
the classes and tables.
You can run paster shell first and explore the models a little bit with
some queries or updating the data. I'm by no means an expert but this
part was really easy to set up.
Mariano
def init_model(engine):
"""Call me before using any of the tables or classes in the model"""
## Reflected tables must be defined and mapped here
global t_users
global t_ticket
t_users = sa.Table("users", meta.metadata, autoload=True,
autoload_with=engine)
orm.mapper(User, t_users)
t_ticket = sa.Table("ticket", meta.metadata, autoload=True,
autoload_with=engine)
orm.mapper(Ticket, t_ticket,
properties={'usertickets1': orm.relation(User, primaryjoin=
t_ticket.c.user_id==t_users.c.id,
foreign_keys=[t_users.c.id]),
'usertickets2': orm.relation(User, primaryjoin=
t_ticket.c.responsible_user_id==\
t_users.c.id,
foreign_keys=[t_users.c.id]),
'usertickets3': orm.relation(User, primaryjoin=
t_ticket.c.create_by==t_users.c.id,
foreign_keys=[t_users.c.id]),
'usertickets4': orm.relation(User, primaryjoin=
t_ticket.c.change_by==t_users.c.id,
foreign_keys=[t_users.c.id])
})
meta.Session.configure(bind=engine)
meta.engine = engine
## Classes for reflected tables may be defined here, but the table and
## mapping itself must be done in the init_model function
t_users = None
class User(object):
pass
t_ticket = None
class Ticket(object):
pass
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.