On Jul 4, 1:45 pm, Rick Hultgren <[email protected]> wrote:
> ...with other words -- how to add fields to a table that is already
> declared? Is it possible?

If they are tables which were created by web2py, then the db.table
attribute is (by default) set for automatic migration - adding and
deleting fields works (but:   deleting a field _will_ generate an
alter table   SQL statement, so any data in those deleted fields will
likely be lost).

>
> On 7/4/10, Rick Hultgren <[email protected]> wrote:
>
> > perhaps the table names will be changed, so please don't bother about
> > "before" and "after"...the point is -- how to refer/link to the table
> > name "locus", when that table is not yet created/declared/made?
>
> > On 7/4/10, Rick Hultgren <[email protected]> wrote:
> >> Thanks for the answers. I'm trying to create these tables:
>

I would (again) suggest you draw the relationships - to simplify this,
notice you do not need double-linked references (hardly ever) - leave
this for a later stage of development (it is something you can do to
try to improve performance).

If you want to know all the components that refer to record (id) # 7
of db.substance, then you would (for example) search thus:

db.( db.process.substance_id == db.substance.id).select(ALL)

Also, I will re-introduce what I think will help you think about this
- call all reference fields in a way that emphasizes that they are
references, by referred table id.

Since you still haven't diagrammed out the relationship (two-way links
can mask the true relationship needed) - I will guess at parent/child
(or responsible / dependent)  relations;  if I get them wrong, then
you will know for your application how to invert them.   I'll assume
hierarchy suggested by the order of table definitions you have given.

> >> db.define_table( 'substance',
> >>  Field( 'substance',  db.substance),
> >>  Field( 'process',  db.process),
> >>  Field( 'condition',  db.condition),
> >>  Field( 'locus',  db.locus),
> >>  Field( 'name'),
> >>  Field( 'main_name'))

db.define_table('substance',  Field('name'), Field('main_name'))   #
all that's needed for this;
>
> >> db.define_table( 'process',
> >>  Field( 'substance',  db.substance),
> >>  Field( 'process',  db.process),
> >>  Field( 'condition',  db.condition),
> >>  Field( 'locus',  db.locus),
> >>  Field( 'name'),
> >>  Field( 'main_name'))

db.define_table('process', Field('name'), Field('main_name'),
   # now only add references to tables "above":
  Field('substance_id', db.substance))   #  a process related to a
substance??

>
> >> db.define_table( 'condition',
> >>  Field( 'substance',  db.substance),
> >>  Field( 'process',  db.process),
> >>  Field( 'condition',  db.condition),
> >>  Field( 'locus',  db.locus),
> >>  Field( 'name'),
> >>  Field( 'main_name'))

# assuming that the condition cannot be cross-lined to one process,
but a different substance;  need clear model to show what is really
needed
db.define_table('condition',  Field('name'), Field('main_name'),
   Field('process_id', db.process))

>
> >> db.define_table( 'locus',
> >>  Field( 'substance',  db.substance),
> >>  Field( 'process',  db.process),
> >>  Field( 'condition',  db.condition),
> >>  Field( 'locus',  db.locus),
> >>  Field( 'name'),
> >>  Field( 'main_name'))

# this could be related to either process or condition..... I'll just
assume process for now
db.define_table('locus',  Field('name'), Field('main_name'),
  Field('process_id', db.process))


This is the kind of relationships I would expect you might start with
(and something that would give you all the information you needed;)

Perhaps this is helpful.

Regards,
- Yarko

>
> >> On 7/4/10, mdipierro <[email protected]> wrote:
> >>> Could you explain to us in English what you are trying to model? I am
> >>> sure there is a simpler solution using link tables.
>
> >>> On 3 Lug, 12:14, Rick Hultgren <[email protected]> wrote:
> >>>> Thanks for the link. My actual problem is more complicated than the
> >>>> example I gave. I realize that I need to link a field to a table that
> >>>> isn't initiated yet (item2):
>
> >>>>         objects = ['substance', 'process', 'condition', 'locus']
> >>>>         linksubobjects = ['locus', 'before', 'after']
> >>>>         unlinksubobjects = ['name', 'main_name']
> >>>>         for object in objects:
> >>>>                 for item1 in linksubobjects+unlinksubobjects:
> >>>>                         for item2 in objects:
> >>>>                                 fields=[Field(item1, 'reference
> >>>> item2')]
> >>>>                                 db.define_table(object,*fields)
>
> >>>> I suppose the solution would be to initiate the table names and then
> >>>> add the fields either in db.py, or in the controller file. But I have
> >>>> no idea how to do this.
>
> >>>> On 7/3/10, Yarko Tymciurak <[email protected]> wrote:
>
> >>>> > The online book has a fairly useful search;
>
> >>>> > For your question, see:
> >>>> >http://web2py.com/book/default/section/6/13
>
> >>>> > Regards,
> >>>> > - Yarko
>
> >>>> > On Jul 3, 8:45 am, Rick <[email protected]> wrote:
> >>>> >> Hi,
>
> >>>> >> I would like to link a table to its own model like this:
>
> >>>> >>         db.define_table('person',
> >>>> >>                 Field('name'),
> >>>> >>                 Field('child', db.person))
>
> >>>> >> I understand that I should first initiate the table and then link it
> >>>> >> to itself. But how to do that?
>
> >>>> >> Thanks in advance for help!

Reply via email to