On 7/4/15 5:09 PM, Chris Norman wrote:
Hi all,
My name is Chris Norman, and I am a hobbyist programmer in the UK.
Firstly, please let me thank the creators for a truly wonderful piece
of software! I was starting my application and coding the SQL by hand,
which was a pain considering I don’t really know SQL all that well.
SQLAlchemy actually gets around a load of problems I was having with
the initial idea.
Anyways, now for my problem:
I’m trying to make a Pythonic sort of copy (with the view to splitting
off) of the popular Lambda MOO server. I’m using a proper database
backend, so that certain things which are a major pain in MOO will
become much easier. That, and it would be cool to have a threaded
version of MOO, without all the annoying bits.
Anyways, my question is this:
I created my tables with declarative and all that - works fine in
fact. But just tonight I added a new column to one table. When I did
Base.metadata.create_all(), I thought SQLAlchemy would realise there’s
an added column, and add it to the database for me, but it hasn’t. Is
there a way to make it do that so I don’t have to do any clever things
when the server loads to check for differences between the database
and the objects I’ve created?
this is a bigger issue in relational databases for which you need to run
specific commands on the database to add new columns and features.
From an application point of view we call this "schema migrations" and
the current tool recommended for this is Alembic:
http://alembic.readthedocs.org/
There's a blurb on the whole subject here:
http://docs.sqlalchemy.org/en/rel_1_0/core/metadata.html#altering-schemas-through-migrations
as for sqlalchemy-migrate I am actually one of the maintainers for it
now and while we have to keep it running for Openstack, it's not by any
means under "active development" at this point that paragraph is out of
date.
Secondly, and sort of incidentally because I’ve already worked around
this, is there any way to return an object from an id without having
to use session.get? I kind of have this in mind:
I'm assuming you mean query.get(), session i think may have had a get()
method at some point but it's long removed. You would just
query.filter(object.id == <foo>), but below it seems like that's not
what you mean.
class Object(Base):
"""My standard object."""
__tablename__ = 'objects'
id = Column(Integer, primary_key = True, nullable = False) # The
primary key.
location = Column(MagicType, default = None) # Location field.
The MagicType would contain an integer database-side, like 1, or 2,
whatever, but would map directly to the object with that id.
I have gotten round this by using:
@property
def location(self):
return self.location.id if self.location else None
@location.setter
def location(self, value):
self._location = value.id if value else None
But I was wondering if there was a type which would do this with less
properties being defined?
so.....are we saying here, get() is OK, but it's the fact that you need
to know the name "MyClass.id" ? I'm not really understanding what this
@property does. A brief but complete example of use would be helpful.
Cheers for all the help, and thanks once again for SLQAlchemy, it
looks mighty fine to me! :-)
Cheers, and take care.
Chris Norman.
--
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.