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

MySQL one-to-one Uncaught error can't create table (errno 150 foreign key constraint is incorrectly formed) #75

Open
EktwpMtz opened this issue Jul 12, 2020 · 3 comments

Comments

@EktwpMtz
Copy link

EktwpMtz commented Jul 12, 2020

(Sorry for my bad English)
I've tried to define a one to one relationship and although it works if I use sqlite it doesn't work if I use MariaDB, this code throws an Uncaught error at db.sync()

class Service extends Model {
    static table = "service";
    static timestamps = true;

    static fields = {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            unique: true,
            allowNull: false
        }
    }
 }

 class Plan extends Model {
    static table = 'plan';
    static timestamps = true;

    static fields = {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            unique: true,
            allowNull: false
        },
        serviceId: Relationships.belongsTo(Service),
    }

    static service() {
        return this.hasOne(Service);
    }

    public id!: number;
    public serviceId!: number;
 }
 db.link([Service, Plan]);
 await db.sync({ drop: true });
@Frenzoid
Copy link

I see that youre using serviceId: Relationships.belongsTo(Service) could you please tell me if the error youre getting is this one?: #76

Best regards.

@eveningkid
Copy link
Owner

Hello Héctor,

Thanks for reporting this issue.

As @Frenzoid mentioned, it would be helpful to get more information regarding your error: what does it say in the console?

Let us know! :)

@EktwpMtz
Copy link
Author

EktwpMtz commented Jul 16, 2020

The console displays this:

error: Uncaught Error: can't create tablet `x`.`plan` (errno: 150 "Foreign key constraint is incorrectly formed")
        throw new Error(error.message);
        at Connection.nextPacket (connection.ts:97:17)
        at async Connection.execute(connection.ts:162:19)
        at async client.ts:101:14
        at async Client.useConnection(client.ts:111:22)
        at async Client.execute(client.ts:100:12)
        at async MySQLConnector.query(mysql-connector.ts:57:22)
        at async Database.query(database.ts:154:21)
        at async Function.createTable(model.ts:120:5)
        at async Database.sync(database.ts:124:7)

I also checked the error #76 and it's not the same

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

3 participants