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

PRIMARY KEY without AUTOINCREMENT #39

Open
VincentJousse opened this issue Oct 5, 2015 · 4 comments
Open

PRIMARY KEY without AUTOINCREMENT #39

VincentJousse opened this issue Oct 5, 2015 · 4 comments

Comments

@VincentJousse
Copy link

Hi,

I need a database table whose primary key will be a UUID saved in a STRING.
The TableBuilder adds automatically the AUTOINCREMENT keyword while it is only allowed on an INTEGER PRIMARY KEY.

An annotation parameter may be added to the "@id" annotation to make the PRIMARY KEY automatically generated or not.

@TimotheeJeannin
Copy link
Owner

Keep in mind that SQLite supports a limited subset of the ALTER TABLE command and column modification is not supported. If we had a @Id(autoincrement = false) annotation, a developer would be able to modify the @Id annotation after the initial table creation to @Id(autoincrement = true). The change would have no effect as we can't modify an existing column. So if we want the annotated contract to actually reflect the table schema we need to re-create the table with the AUTOINCREMENT option and provide to the developer a way to handle all the already present data that conflicts with the new primary key column. This will lead to some complicated logic and it will be difficult to understand what does ProviGen do and when.

I feel like it makes more sense to modify the TableBuilder so that it can handle the AUTOINCREMENT option and let the developer manage table creation himself.

@VincentJousse
Copy link
Author

The same issue exists for the @id annotation itself, right ?!! If a developer moves the @id annotation from one field to another, we would have to re-create the table ...

@TimotheeJeannin
Copy link
Owner

Hi @VincentFTS,

If you move an @id annotation, ProviGen will continue to work and you don't necessarily need to re-create the table. Basically the last segment of the uri would be used to match the newly annotated column.

If you really need to create the initial table differently, you can just use your own SQLiteOpenHelper.

public class MyContentProvider extends ProviGenProvider {

    @Override
    public Class[] contractClasses() {
        return new Class[]{MyContract.class};
    }

    @Override
    public SQLiteOpenHelper openHelper(Context context) {

        return new SQLiteOpenHelper(getContext(), "databaseName", null, 1) {

            @Override
            public void onCreate(SQLiteDatabase database) {
                // Create the table without the AUTOINCREMENT here.
            }
        };
    }
}

Another option would be to fork ProviGen and modify the TableBuilder so that the AUTOINCREMENT is optional.

@TimotheeJeannin
Copy link
Owner

@VincentFTS Did you find a solution to your problem ?

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

2 participants