On 9/8/15 12:57 PM, Steve Murphy wrote:
A bit difficult is grabbing just certain columns in the select, given
that we have only a list of column names. That would be real nice
if such a method were available in the core API.
For example, a method for select whereby I could supply a simple
list of column names to fetch in the select would be very handy:
tablename = "user"
tab = meta1.tables[tablename]
collist = [ "id", "name", "address" ] ## just a few of many more
q = tab.select(colnames=collist)
this is pretty simple:
q = select([tab.c[name] for name in ["id", "name", "address"]])
Also, if I could get a column object from a table by name
it would really make life easier at times:
q = select([tab]).where(tab.colname(colnamevar) == col_var_val)
yeah, tab.c[name]
this is here:
http://docs.sqlalchemy.org/en/rel_1_0/core/metadata.html#accessing-tables-and-columns
Another rough spot with the API is setting up a simple set of where
clauses for a select, given a map of column names vs. values.
if the map contained {"user": "cat", "city": "Gotham"} it would
be cool if we could get the desired select:
select * from table where user='cat' and city='Gotham';
that is:
select = select.where(and_(*[tab.c[key] == value for key, value in
mymap.items()]))
Another rough spot is getting a list of constraints. I note that
postgresql has sql to create constraints, and remove them, but
not to get a list of them.
you can get these like this:
from sqlalchemy import inspect
insp = inspect(my_engine)
fk_constraints = insp.get_foreign_keys('mytable')
uq_constraints = insp.get_unique_constraints('mytable')
indexes = insp.get_indexes('mytable')
docs:
http://docs.sqlalchemy.org/en/rel_1_0/core/reflection.html#fine-grained-reflection-with-inspector
there's no listing of CHECK constraints right now.
they are also available on the Table as fully constructed constraint
objects:
table = Table('mytable', somemetadata, autoload=True)
for constraint in table.constraints:
# ...
docs for the table.constraints accessor are not in good shape right now,
the API docs aren't generating for it and there's only a few mentions of
it, but inspector is usually more straightforward if you are just
getting raw information about them.
I do see the "\d <table-name>" command,
and you get a list of all the foreign keys and constraints that way,
all in a big block of text.... I'm sure that, without supporting sql
syntax,
any further sqlalchemy constraint support will be very hard, if not
impossible,
to implement.
well \d is just part of your psql shell, it is ultimately querying the
pg_catalog tables which is what the SQLA postgresql dialect queries as well.
The fact that sqlalchemy does what it does has saved me a ton of time
writing
a full-blown SQL parser. Many congrats, thanks, ataboys, and kudu's!!!
thanks glad its working out!
murf
--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.