Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Question Regarding Shard Keys #53

Answered by carlsverre
devingray asked this question in Q&A
Discussion options

I have a table defined like this in Laravel

Schema::create('user_mails', function (Blueprint $table) {
 $table->bigIncrements('id');
 $table->unsignedBigInteger('user_id');
 $table->string('template_id');
 $table->longText('data');
 $table->string('response_status_code');
 $table->longText('response_message');
 $table->timestamps();
 });

I want to add a shardkey on the 'user_id' column

So I added this after the timestamps as per the docs

$table->shardKey('user_id');

I get the following error

Incorrect table definition; there can be only one auto column and it must be defined as a key


My Question is, what would the correct way to add these shard keys be? I can do it directly in singlestore using

CREATE TABLE `user_mails` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` bigint(20) unsigned NOT NULL,
 `template_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `data` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
 `response_status_code` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `response_message` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
 `created_at` timestamp NULL DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,
 KEY (`id`) USING HASH,
 SHARD KEY(`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

And this works however I need to map this to a migration file

Would this be the correct solution?

$table->id()->withoutPrimaryKey();
$table->unsignedBigInteger('user_id')->shardKey();
...
$table->primary(['id', 'user_id']);
You must be logged in to vote

It seems like the correct answer is something like this:

 $table->bigIncrements('id')->withoutPrimaryKey()->index();
 $table->unsignedBigInteger('user_id')->shardKey();
 $table->string('template_id');
 $table->longText('data');
 $table->string('response_status_code');
 $table->longText('response_message');
 $table->timestamps();

Added as a test for future reference:
e6fd63b#diff-f6da6c22d5e74ab81179bda2693633c2fb36619a61c3d97f857875f906c8fa1bR59

Replies: 1 comment

Comment options

It seems like the correct answer is something like this:

 $table->bigIncrements('id')->withoutPrimaryKey()->index();
 $table->unsignedBigInteger('user_id')->shardKey();
 $table->string('template_id');
 $table->longText('data');
 $table->string('response_status_code');
 $table->longText('response_message');
 $table->timestamps();

Added as a test for future reference:
e6fd63b#diff-f6da6c22d5e74ab81179bda2693633c2fb36619a61c3d97f857875f906c8fa1bR59

You must be logged in to vote
0 replies
Answer selected by carlsverre
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /