Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration for Table creation #174

Closed
sergiocampama opened this issue Jan 23, 2014 · 9 comments
Closed

Migration for Table creation #174

sergiocampama opened this issue Jan 23, 2014 · 9 comments

Comments

@sergiocampama
Copy link

Hi, I have a problem where a new class is not generating the table it needs. What I want to do to fix it is to create a migration that creates the table. I have been reading Active Android's source trying to find the place you create the tables, but I haven't been able to find it.

I am fine with the basic type columns, but I don't know how to create the columns that reference other objects, mainly because I don't know how the foreign keys are created in ActiveAndroid. What my migration has right now is

create table if not exists Items (Id integer primary key autoincrement, WebId integer, 
  Name text, GuideQuantity integer, DispatchedQuantity integer, Dispatch integer,
  foreign key(Dispatch) references Dispatches(Id));

The class represented by this is

@Table(name = "Items")
public class Item extends BeetrackModel {

    @Column(name = "WebId")
    public Long webId;

    @Column(name = "Name")
    public String name;

    @Column(name = "GuideQuantity")
    public Long guide_quantity;

    @Column(name = "DispatchedQuantity")
    public Long dispatched_quantity;

    @Column(name = "Dispatch")
    public Dispatch dispatch;
}

The Dispatch column has a foreign key to the Id column of the Dispatches table. How is ActiveAndroid generating those foreign keys so I can recreate it as close to how it should be?

Thanks and best regards,

@vonloxley
Copy link
Contributor

New classes should be generated automatically if you increase the database version number, either through Configuration or AA_DB_VERSION meta-data. If your class is not a direct ancestor of model, it might be a bug in one of the new features.

@vsigler
Copy link
Contributor

vsigler commented Jan 24, 2014

I somewhat lost overview of the codebase in the recent development boom, but originally the tables were created only when creating the database. For any subsequent versions you had to write a conversion/migration script.

I'd say as long as it is a valid SQL, AA will not mind. :) However, bear in mind that the foreign keys are not supported in earlier android versions (API < 11 if I'm not mistaken), so if the script runs on such android version, you'll have problems.

@vonloxley
Copy link
Contributor

The documentation states: "If new classes are added, ActiveAndroid will automatically add them to the database". And I’m pretty shuere it works on my (old) codebase. Foreign keys should best be left to AA, as it decides itself whether to use them or not.

@vsigler
Copy link
Contributor

vsigler commented Jan 24, 2014

Yes, you're right, I did a quick check of the codebase and it really should be adding the tables. It's probably a bug then.

@sergiocampama
Copy link
Author

Damn, it's my fault, I thought that the version number should only change when a migration with column changes to already existing tables is added, not when adding models (tables), which in hindsight was a pretty dumb assumption.

But I'm really intrigued as to where the tables get created, I couldn't find it in the code base. :S

Thanks!

@mario20340
Copy link

I am trying to add new table to my db, i created new class (extends Model) and icnreased db version in Manifest but it seems that active android didn't create new table... i get no such table exception when trying to work with that table...

Can you confirm that activeAndroid automatically creates new tables without creating migration scripts as it says in documentation... is it tested well

@joshuapinter
Copy link
Contributor

joshuapinter commented Jan 30, 2014

I believe you have to bump up the database version number in your manifest file for the new table to be created.

@mario20340
Copy link

as i said i did that multiple times... nothing happens
On 30 Jan 2014 22:01, "joshuapinter" notifications@github.com wrote:

I believe you have to bump up the database version number in your manifest
file for the new table to be created.

Joshua Pinter

On Thursday, January 30, 2014 at 12:16 PM, mario20340 wrote:

I am trying to add new table to my db, i created new class (extends
Model) and icnreased db version in Manifest but it seems that active
android didn't create new table... i get no such table exception when
trying to work with that table...
Can you confirm that activeAndroid automatically creates new tables
without creating migration scripts as it says in documentation... is it
tested well

Reply to this email directly or view it on GitHub (
#174 (comment)).

Reply to this email directly or view it on GitHubhttps://github.com//issues/174#issuecomment-33732787
.

@mario20340
Copy link

my bad... i didnt put meta data tags into right place in manifest, sorry guys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants