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

[Feature Request] grouped unique constraint #263

Open
rendyep opened this issue Apr 16, 2022 · 2 comments
Open

[Feature Request] grouped unique constraint #263

rendyep opened this issue Apr 16, 2022 · 2 comments

Comments

@rendyep
Copy link

rendyep commented Apr 16, 2022

Table declaration:

const SqfEntityTable tableAgeGroup = SqfEntityTable(
  tableName: 'ageGroup',
  primaryKeyName: 'id',
  primaryKeyType: PrimaryKeyType.integer_auto_incremental,
  useSoftDeleting: false,
  fields: [
    SqfEntityField(
      'minAge',
      DbType.integer,
      isUnique: true,
      isNotNull: true,
    ),
    SqfEntityField(
      'maxAge',
      DbType.integer,
      isUnique: true,
      isNotNull: true,
    ),
    SqfEntityField(
      'label',
      DbType.text,
      isUnique: true,
      isNotNull: true,
    ),
  ],
);

Test & output:

  var model1 = AgeGroup.withFields(1, 2, "1 to 2");
  await model1.save();
  print(model1.saveResult);
  // ageGroup-> id=1 saved successfully
  
  var model2 = AgeGroup.withFields(1, 2, "1 to 2");
  await model2.save();
  print(model2.saveResult);
  // ageGroup-> Save failed. Error: DatabaseException(error code 19: UNIQUE constraint failed: ageGroup.label) sql 'INSERT INTO ageGroup (id, minAge, maxAge, label) VALUES (NULL, ?, ?, ?)' args [1, 2, 1 to 2]
  
  var model3 = AgeGroup.withFields(1, 5, "1 to 5");
  await model3.save();
  print(model3.saveResult);
  // ageGroup-> Save failed. Error: DatabaseException(error code 19: UNIQUE constraint failed: ageGroup.minAge) sql 'INSERT INTO ageGroup (id, minAge, maxAge, label) VALUES (NULL, ?, ?, ?)' args [1, 5, 1 to 5]

I have tried using isIndexGroup, but the output of model3 indicated that it doesn't work. Probably isIndexGroup is designed only for index grouping.

Request:

  1. Multiple columns unique constraint, would be nice using isUniqueGroup parameter (like the isIndexGroup) or similar parameter name.
@rendyep
Copy link
Author

rendyep commented Apr 16, 2022

it seems like it has been addressed in #128
I just can't get where should I put the isPrimarykey parameter?

@rendyep rendyep changed the title [Feature Request] multiple columns unique constraint [Feature Request] grouped unique constraint Apr 16, 2022
@hhtokpinar
Copy link
Owner

You could remove the primary key id, and just add isPrimaryKey parameters into other keys instead of isUnique like below

SqfEntityField(
      'maxAge',
      DbType.integer,
      isPrimaryKey: true,
      isNotNull: true,
    ),

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