# Extending table to add new columns failed

**URL:** <https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216>\
**Category:** Development\
**Tags:** module\
**Created:** [July 10, 2016, 1:23am UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216 "2016-07-10T01:23:36Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [July 10, 2016, 1:23am UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/1 "2016-07-10T01:23:37Z")

</div>

Hello,

I have a task of extending the cohort\_member table in core within my particular module. I was going through the [wiki](https://wiki.openmrs.org/display/docs/Extending+a+Table+Through+a+Module) page trying to understand how to go about it. I believe I have most of the code in place, but I can’t wrap my head around how to seamlessly integrate the new columns in cohort\_member table, since the table is basically a set that is used in the Cohort model?!

Here is the link to the github repo [GitHub - vshl/openmrs-module-expandedcohort](https://github.com/vshl/openmrs-module-expandedcohort)

Right now, I cannot view the Cohorts in the Cohort builder, here is the error message:

[Log at 2016-07-09 18.20.12.txt](https://talk.openmrs.org/uploads/default/original/2X/2/256190a5a708014d5f9545c315b7f9f61a14aa6c.txt) (13.1 KB)

Feel free to ask me further questions. Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [July 10, 2016, 1:54pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/2 "2016-07-10T13:54:41Z")

</div>

We generally do not recommend modifying core platform tables from modules. You can have the module create its own tables whose names are prefixed with the moduleId. Or you could create a pull request to the core platform with the table modification changes.

The error message you are getting simply means that your database table does not have the “start\_date” column.

[https://wiki.openmrs.org/display/docs/Extending+a+Table+Through+a+Module](https://wiki.openmrs.org/display/docs/Extending+a+Table+Through+a+Module)

---

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [July 11, 2016, 12:38am UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/3 "2016-07-11T00:38:15Z")

</div>

Hi Daniel,

To give little more context, I am following the design directions as mentioned in the following [forum](https://notes.openmrs.org/2016-02-17-Design-Forum). And after some discussion with Darius in this [thread](https://talk.openmrs.org/t/data-model-for-the-revised-cohort-definition/5999), we concluded that I will concentrate my efforts in a module owing to the time constraints and other factors pertaining to the nature of my project.

Do you think, I can achieve the same by duplicating the cohort\_member table within my module and at the same time not break functionality with existing cohorts?

Thanks for the reply,

Vishal

---

<div class="post-metadata">

**Author:** ![burke](https://talk.openmrs.org/user_avatar/talk.openmrs.org/burke/32/17_2.png) [@burke](https://talk.openmrs.org/u/burke)\
**Post date:** [July 11, 2016, 2:01am UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/4 "2016-07-11T02:01:41Z")

</div>

> [@vshankar](#):
>
> Do you think, I can achieve the same by duplicating the cohort\_member table within my module and at the same time not break functionality with existing cohorts?

You either need to make the changes to core (via PR) or extend the core table(s) in your module. Extending a core table (e.g., adding start and stop date to a cohort member) would typically be done with something like:

```sql
create table expandedcohort_cohort_member
  (
    cohort_member_id int PRIMARY KEY,
    start_date datetime,
    stop_date datetime,
    FOREIGN KEY (cohort_member_id) REFERENCES cohort_member (cohort_member_id)
  )

```

Unfortunately, `cohort_member` in core was not designed to persist member entries, so does not have it’s own primary key (it only has [`cohort_id` and `patient_id`](http://resources.openmrs.org/datamodel/openmrs-data-model-1.11.html#cohort_member)). Since rows in `cohort_member` are purged as members are removed and there’s no `cohort_member_id` primary key, you can’t extend the core cohort members in your module as [Darius suggested](https://talk.openmrs.org/t/data-model-for-the-revised-cohort-definition/5999/14). 😖

This really needs to be done in core. The only way to add start & stop dates to cohort members via a module would be to alter the core table (adding a `cohort_member_id` primary key along with start & stop dates) as well as changing core’s behavior with cohort members (currently purging members that are removed). You’ll probably spend more time trying to do this in a module than you would making the changes to core.

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [July 11, 2016, 3:56pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/5 "2016-07-11T15:56:44Z")

</div>

As I mentioned multiple times on the other thread, I recommend that you make these changes in (a feature branch of) openmrs-core.

Because that way you’re less likely to run into other dependencies and complexities, like this one.

---

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [July 18, 2016, 10:46pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/6 "2016-07-18T22:46:38Z")

</div>

Thanks Darius and Burke. After discussing with my peer group. I’ll try to make the changes in the core itself. Are there some development guidelines that I need to follow to bring forth the change within core?

---

<div class="post-metadata">

**Author:** ![burke](https://talk.openmrs.org/user_avatar/talk.openmrs.org/burke/32/17_2.png) [@burke](https://talk.openmrs.org/u/burke)\
**Post date:** [July 19, 2016, 2:25am UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/7 "2016-07-19T02:25:17Z")

</div>

If you haven’t already, review the [GitHub Code of Conduct](https://wiki.openmrs.org/x/aoUvAg) and [Coding Conventions](https://wiki.openmrs.org/x/MxEz). In general, it goes something like this:

- Fork openmrs-core
- Make a branch (usually named after the ticket)
- Get feedback from other devs as need via [IRC](http://om.rs/irc) or [Telegram](http://om.rs/tg).
- When ready, submit a PR
- After it is approved, squash your commits into one commit (if multiple commits)

---

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [July 22, 2016, 8:38pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/8 "2016-07-22T20:38:03Z")

</div>

Thanks Burke. That’s been useful. Do I have to create the associated ticket in JIRA? I will be forking the openmrs-core repo to my github and will create a feature branch.

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [July 22, 2016, 9:01pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/9 "2016-07-22T21:01:25Z")

</div>

@vshankar, sure, go ahead and create a ticket in the “OpenMRS Core” project in JIRA, and share a link to it here. (Someone will mark it ready-for-work and you can claim it.)

In this case I would name the branch something more descriptive, like “expanded-cohort”, but using the ticket number is okay too.

---

<div class="post-metadata">

**Author:** ![ssmusoke](https://talk.openmrs.org/user_avatar/talk.openmrs.org/ssmusoke/32/8020_2.png) [@ssmusoke](https://talk.openmrs.org/u/ssmusoke)\
**Post date:** [July 23, 2016, 8:01am UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/10 "2016-07-23T08:01:41Z")

</div>

@vshankar even better is to name the branch TicketNumber-expanded-cohort (that way you have the best of both worlds)

---

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [July 25, 2016, 11:23pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/11 "2016-07-25T23:23:39Z")

</div>

Sorry it took long, I had some permission issue before I could create an issue. Here is the newly created ticket [https://issues.openmrs.org/browse/TRUNK-4906](https://issues.openmrs.org/browse/TRUNK-4906).

---

<div class="post-metadata">

**Author:** ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)\
**Post date:** [July 26, 2016, 4:26pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/12 "2016-07-26T16:26:02Z")

</div>

@vshankar I marked the ticket as ready-for-work, so you can now go “claim” it.

---

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [July 26, 2016, 4:43pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/13 "2016-07-26T16:43:57Z")

</div>

Thanks for the help, Darius. Much appreciated.

---

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [August 3, 2016, 5:08am UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/14 "2016-08-03T05:08:43Z")

</div>

@burke @darius I’ve been reviewing the openmrs-core code, and I hope the instructions to extend cohort\_member table in the [Design Forum](https://notes.openmrs.org/2016-02-17-Design-Forum) still stand?

I am trying to gather the requirements to add the additional columns for cohort\_member. I have added the additional columns in liquibase and removed the Unique constraint. Now the tricky portion of cohort\_member being a collection mapping to Cohort, how does the additional columns (start\_date, end\_date) fit in within this mapping? Do we keep the mapping as it is or will this change?

Also, the two methods Cohort.getMembers(Date asOfDate) and Cohort.getMemberChanges(Date fromDate, Date toDate): will these two methods just be SQL query calls to return the list of Cohorts?

Thanks.

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [September 6, 2016, 9:18pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/15 "2016-09-06T21:18:03Z")

</div>

@vshankar did you get answered on this thread? [Review of extending cohort\_member table with new columns](https://talk.openmrs.org/t/review-of-extending-cohort-member-table-with-new-columns/7760/5)

---

<div class="post-metadata">

**Author:** ![vshankar](https://talk.openmrs.org/letter_avatar/vshankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@vshankar](https://talk.openmrs.org/u/vshankar)\
**Post date:** [September 6, 2016, 9:38pm UTC](https://talk.openmrs.org/t/extending-table-to-add-new-columns-failed/7216/16 "2016-09-06T21:38:15Z")

</div>

Yes, I did. Thanks. I’m currently working on it.
