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

Unique constraint on multiple columns #63

Merged
merged 1 commit into from
Nov 6, 2016

Conversation

MyDogTom
Copy link
Contributor

Addresses issue #54

Two ways to specify unique constraint on table level

@UniqueConstraint(columns = {TagColumns.NOTE_ID, TagColumns.NAME}, onConflict = REPLACE)
public interface TagColumns {

or

@Constraints(
    @UniqueConstraint(columns = {TagColumns.NOTE_ID, TagColumns.NAME}, onConflict = REPLACE)
)
public interface TagColumns {

The last one allows to specify multiple unique constraints.
Full example. Table definition:

@Constraints(
    @UniqueConstraint(columns = {TagColumns.NOTE_ID, TagColumns.NAME}, onConflict = REPLACE)
)
public interface TagColumns {
  @DataType(Type.INTEGER)
  @PrimaryKey
  @AutoIncrement
  String ID = "_id";

  @DataType(Type.INTEGER)
  @NotNull
  @References(table = NotesDatabase.NOTES, column = NoteColumns.ID)
  String NOTE_ID = "note_id";

  @DataType(Type.TEXT)
  @NotNull
  String NAME = "name";
}

The generated code will looks like:

public static final String NOTES_TAGS = "CREATE TABLE IF NOT EXISTS notes_tags ("
   + TagColumns.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
   + TagColumns.NOTE_ID + " INTEGER NOT NULL REFERENCES notes(_id),"
   + TagColumns.NAME + " TEXT NOT NULL,"
   + " UNIQUE ( note_id, name ) ON CONFLICT REPLACE)";

@WalkerWalker
Copy link

WalkerWalker commented Oct 31, 2016

wowowowow. This looks super exciting. Thanks! Actually I just posted some thoughts and was hoping for one pull request because this is certainly out of my scope....

And suddenly, boom, there you are.

btw, how does the master branch handles possible different on conflict constrains in the multiple primary keys case?

Copy link
Owner

@SimonVT SimonVT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! I just have two small changes I'd like to make :)

*/
@Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE)
public @interface Constraints {
UniqueConstraint[] value();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about naming this unique instead of value? Then after this change is merged we can add the other table constraints.


/** Adds the UNIQUE constraint on a table level */
@Retention(CLASS) @Target(TYPE)
public @interface UniqueConstraint {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an optional constraint name.

@MyDogTom
Copy link
Contributor Author

MyDogTom commented Nov 5, 2016

Done. Please, take another look. Let me know if I need to squash commits.
Regarding other constraints - I can do it. I'll create a separate issue for this. I'd like to know your thoughts about PrimaryKey.

@SimonVT
Copy link
Owner

SimonVT commented Nov 6, 2016

Looks good, please squash and I'll get it merged :)

@MyDogTom
Copy link
Contributor Author

MyDogTom commented Nov 6, 2016

squashed

@SimonVT SimonVT merged commit 5fa0fcb into SimonVT:master Nov 6, 2016
@SimonVT
Copy link
Owner

SimonVT commented Nov 6, 2016

Thanks!

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

Successfully merging this pull request may close these issues.

3 participants