简体   繁体   中英

Is there a better way to deal with unique field in yii2 curd form?

In my yii2 form I have a field name number , which is unique in DB.

This number will be used for sorting in frontend.

Whats the best way to deal with this field in backend?

I guess you're looking how to create a primary key ? primary key must be unique here I declare number_field as primary key which is unique.

Here is an example of a table creation:

CREATE TABLE `table_name` (
 `number_field` bigint(20) NOT NULL,
 `another_field` bigint(20) DEFAULT NULL,
 `extra_field` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`number_field`),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

also you can use UNIQUE KEY prop instead of PRIMARY KEY

you can change existing table like this

alter table 
your_table_name 
add unique (number_field)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM