That would involve some code like this (not sure if it's easier to
write this code,
than to write plain sql statements :) ):
metadata = MetaData(migrate_engine)
person = Table('person', metadata, Column('id', Integer))
person_column = Column('person_id', Integer, ForeignKey('person.id',
onupdate='cascade'), nullable=False)
movie = Table('movie', metadata, person_column)
person_constraint = ForeignKeyConstraint(['person_id'], ['person.id'],
ondelete="restrict", table=movie)
def upgrade():
create_column(person_column, movie)
person_constraint.create()
def downgrade():
person_constraint.drop()
drop_column(person_column, movie)
On Jun 16, 11:37 am, "Giovanni Marco Dall'Olio" <[email protected]>
wrote:
> On Mon, Jun 15, 2009 at 9:33 PM, erikj <[email protected]> wrote:
>
> > I would advise the use of sqlalchemy-migrate, it takes some
> > time to get it up and running and get used to it, but it's well
> > worth the effort.
>
> Hi,
> thank you very much, sqlalchemy-migrate seems useful and the linked
> documentation is very interesting.
>
> However, this doesn't solve all of my problems... migrate requires you to
> write an upgrade() and downgrade() functions, to describe the changes
> introduced in a version of the database; but I not able to write such
> functions.
>
> For example, let's say I have a db with a single table:
>
> class Person(Entity):
> name = Field(Text)
> surname = Field(Text)
>
> and I want to add an additional table, with a OneToMany relationship:
>
> class Person(Entity):
> name = Field(Text)
> surname = Field(Text)
> favorite_movie = OneToMany('Movie')
>
> class Movie(Entity):
> title = Field(Text)
> fans = ManyToOne('Person')
>
> now, how would you do the migration without dropping down the table, and
> loosing all the data in it?
> I could manually alter the Person table, but I am not sure on what would be
> exactly the structure that elixir expects.
> For the moment I have solved this by only using ManyToMany (which may be
> good for what I need to do), but I wonder if there is some guideline for
> doign this with elixir, or if you have any suggestion.
>
> cheers
>
>
>
>
>
> >http://code.google.com/p/sqlalchemy-migrate/
>
> > The nice thing is that it keeps track of the 'revision' of
> > your database schema. So you can develop everything against
> > your test db, and once you put your app in production at
> > multiple sites or at new sites, you just run the upgrade command,
> > and your db is ready.
>
> > for difficult migrations, you can always use plain sql
> > commands using engine.execute('alter table ...')
>
> > as for the recipe on the elixir website on using sqlalchemy-migrate,
> > it's not really complete...
>
> > regards,
>
> > Erik
>
> > On Jun 15, 5:11 pm, "Giovanni Marco Dall'Olio" <[email protected]>
> > wrote:
> > > Hi,
> > > this question has been already asked in this list one year ago, but I
> > would
> > > like to know if it has changed in the mean time.
>
> > > So, what is the best way to alter a table structure with elixir?
> > > In particular, I want to add a new table, and some OneToMany and
> > ManyToMany
> > > relationships to it in the existing tables.
> > > This could be done manually, but I am not sure on how to do it and keep
> > it
> > > compatible with elixir and how to make elixir recognize it later.
>
> > > --
> > > Giovanni Dall'Olio, phd student
> > > Department of Biologia Evolutiva at CEXS-UPF (Barcelona, Spain)
>
> > > My blog on bioinformatics:http://bioinfoblog.it
>
> --
> Giovanni Dall'Olio, phd student
> Department of Biologia Evolutiva at CEXS-UPF (Barcelona, Spain)
>
> My blog on bioinformatics:http://bioinfoblog.it
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---